kit
kit copied to clipboard
Add fetch method to ServerLoadEvent similar to LoadEvent to address same issues
Describe the problem
The implementation of fetch for LoadEvent has a number of benefits highlighted here - https://kit.svelte.dev/docs/load#input-methods-fetch.
Would it make sense to have a similar implementation of fetch for ServerLoadEvent? There are many cases where this would be beneficial where you have +page.server.ts functionality accessing an internal REST API:
- Using relative URLs
- Credentialed requests on the server - inherits the cookie and authorization headers from the original page request
- Internal requests go direct to the handler function when running on the server, without the overhead of an HTTP call
Describe the proposed solution
A fetch method for ServerLoadEvent that works exactly the same ways LoadEvent.
Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
Yes, this is something we've been meaning to get round to
Making a note-to-self that when we do this, we can get rid of the hacky global fetch
override during prerendering: https://github.com/sveltejs/kit/blob/05d532bbdf4cf4e857bd74e53799f671fb4cd152/packages/kit/src/core/prerender/prerender.js#L96-L147
I've just closed #6730 as a duplicate of this, although that was for fetch
in +server.js
. If we add fetch
to RequestEvent
instead of to ServerLoadEvent
, we could handle that as well.
Yep, this should be for both +server.js
and +page.server.js
.
Another thing that the special SvelteKit fetch
does that the OP here didn't mention (which I did in #6730), which could be useful in +server.js
/+page.server.js
is the fact that it will go through the handleFetch
hook. It'd be useful to have that functionality in server side fetches as well.
This would be very awesome. I'm new to svelte but I'm using it in three projects for my company. I was trying to recover myself from all the changes made to the framework recently when I realized that were no fetch in *.server.ts :) but I'm using it a lot in forms where in +page.server.ts, after validation, I send data to the be.