Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable $fetch function call for prerendered api routes #24311

Open
3 of 4 tasks
Rigo-m opened this issue Nov 15, 2023 · 4 comments
Open
3 of 4 tasks

Disable $fetch function call for prerendered api routes #24311

Rigo-m opened this issue Nov 15, 2023 · 4 comments

Comments

@Rigo-m
Copy link
Contributor

Rigo-m commented Nov 15, 2023

Describe the feature

$fetch function calling capabilities in SSR is cool, but when I prerender an expensive route via routeRules I don't want the useFetch composable / $fetch calls to re-run the computation, I want to return the prerendered response.

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

@manniL
Copy link
Member

manniL commented Nov 15, 2023

Not sure what exactly you mean 🤔

While perendering, useFetch/useAsyncData has to run to ensure that the correct content is fetched (unless set to server: false, but then it is not part of the server-rendered content).

@Rigo-m
Copy link
Contributor Author

Rigo-m commented Nov 15, 2023

Let's say you have /api/fetchSomething.ts that takes 10 whole seconds to fetch that data.
You then move to routeRules and you set { '/api/fetchSomething': { prerender: true }

At that point, that API gets prerendered and transformed as a plain text file during your build, but:

  • If you fetch that route externally or during CSR: instant response with the prerendered content ensues (yay! 🎉 )
  • If you have a useFetch inside a component setup, and that component/page gets SSR, the expensive computation gets called (so the page takes 10+ seconds to render

@danielroe
Copy link
Member

That's a helpful explanation. Our 'internal' fetch should fetch from public assets rather than via the handler in this case. (We should probably also strip the handler from the server bundle to improve performance.)

cc: @pi0

@Rigo-m
Copy link
Contributor Author

Rigo-m commented Nov 15, 2023

Willing to help with the implementation if you're ok with it

antoinerey added a commit to antoinerey/nuxt that referenced this issue Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants