Make serverMiddleware available when generating static pages

bdadam
0
bdadam
commented a month ago

What problem does this feature solve?

Some server side data APIs are provided as serverMiddleware. These middleware can be accessed both on client side and also on server side in asyncData when either running nuxt start or nuxt dev. This is a quite convenient way to access data needed for pages in asyncData.

Unfortunately none of the serverMiddleware seems to be working during static generation when running nuxt generate.

If the serverMiddleware were accessible during static file generation:

  • The page's asyncData could have access to the initial data. There was no need for providing payload in the generate method in nuxt.config.js.
  • With some logic in the serverMiddleware we could "record" the response to the dist folder in json files for the static files to access them (when no server is available)

I think currently the serverMiddleware are not used by design but I don't know the code enough to know why it is that way.

My current mitigation: start an API server on a different port in the nuxt.config.js. This works but feels unnecessary.

What does the proposed changes look like?

The middleware in serverMiddleware are accessible during static generation.

idea
0
Atinux
28.1k
Atinux
commented a month ago

The PR #6159 should also enable to have access to the serverMiddleware when using nuxt generate :)

0
Informations
Feature Request โ€ข Open
#c9978 - Created a month ago