kit icon indicating copy to clipboard operation
kit copied to clipboard

Add fetch method to ServerLoadEvent similar to LoadEvent to address same issues

Open nstuyvesant opened this issue 2 years ago • 5 comments

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:

  1. Using relative URLs
  2. Credentialed requests on the server - inherits the cookie and authorization headers from the original page request
  3. 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

nstuyvesant avatar Aug 26 '22 18:08 nstuyvesant

Yes, this is something we've been meaning to get round to

Rich-Harris avatar Aug 26 '22 21:08 Rich-Harris

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

Rich-Harris avatar Aug 29 '22 19:08 Rich-Harris

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.

Conduitry avatar Sep 11 '22 11:09 Conduitry

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.

AaronFlynn1989 avatar Sep 11 '22 18:09 AaronFlynn1989

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.

ftognetto avatar Sep 20 '22 12:09 ftognetto