-
-
Notifications
You must be signed in to change notification settings - Fork 767
feat: server fetch utils #3731
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
feat: server fetch utils #3731
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
|
Love it! A few suggestions, not sure if implemented:
|
|
@MickL skipping middleware (by default) might not be safe. A guard middleware, for example, can be bypassed from an SSR => API request this way. Also, sometimes middleware are used as a conditional routing handler (they intercept the final response). You might use a specific internal request flag to indicate it or use req.context and use a custom fetch wrapper to do this. Default You can also easily use We should improve |
|
Thanks for the reply. I would still suggest:
Why not:
|
Internal and external fetch behavior are identical, as the
That's why we have But i think IDE issue is not because of import name. It happens for other subpath imports as well.
yes we will add it for paths in the future with an opt-in (typegen) flag.
ofetch adds runtime DX features mainly. Generic does not needs it. For typing |
I think thats a big issue. I already had this problem with Nitro v1 and it was hard to find a difference in the two
Yes thats great! :) Seems like you want to get rid of all features but some DX like ofetch offers always has been nice and would still love to see that without the need to build my own fetch wrapper. Thats why I had the idea, ship (patched) ofetch as an optional import. And if one doesnt want to use it, he can just not import and use native fetch. |
Main benefit of server fetch is that there is no network/TCP round-trip, reducing >ms latency for sub-requests. I think it is always a good idea to implement an HTTP caching mechanism for session, i18n, etc that works universally best (both for internal and external requests). Of course you can add an internal flag to skip layers but i'm worried it can add to attack surface and reduce deployment flexibility (splitting some parts like SSR and APIs will be harder) |
|
Another important usecase for me is to call another function (handler) e.g. when a webhook from Stripe comes in saying "product sold". Now I want to call my product-update-handler Another different solution would be if handlers can be called directly as a function call without fetch. |
Currently nitro patches global fetch with a wrapper that check if request is an string starting with
/, dispatches it as an internal fetch. In nitro v2, this was being done using global$fetchinstead.Additionally vite patches global fetch (again) to allow fetching other environments.
This can cause all sorts of implicit behavior with ordering also having this, adds to core bundle size (~1kB + 1kB more for vite).
This PR introduces two new exports:
serverFetchandfetch.The difference is that,
serverFetchalways dispatches requests to internal routing whilefetchdoes the hybrid method and unless request starts with/, fallbacks to native fetch.Utils are exported from
"nitro"and"nitro/runtime".Exports from /runtime are bound to nitro instance and only usable in runtime. While importing
serverFetchoutside of nitro runtime, allows fetchin server routes in Nitro modules and Vite plugins (currently limited to dev mode and one Nitro instance per process)This PR also updates vite mechanism to avoid wrapper and exporting new
fetchViteEnvutil fromnitro/runtime/vitesubpath.