kit icon indicating copy to clipboard operation
kit copied to clipboard

Expose whether a given `event` is a real network request or a synthetic `fetch` during `load`

Open Conduitry opened this issue 2 years ago • 2 comments

Describe the problem

There is not currently a good way to know whether a given event corresponds to a real request coming in to the server, or a synthetic request from load's fetch (or event.fetch) being called for the same origin, which just calls handle without generating a real network request.

This is important to be able to know in my particular use case of having an API gateway that sites in front of both the SvelteKit app and some external API. load fetch requests from the browser can be set to the same origin, but these requests during SSR get sent to handle and I need them to go out to the real API. Right now, I'm doing this when the URL path matches a given regex - however, this could easily match things that aren't really API requests, and if these are ultimately mapped back to the SvelteKit app, this results in an infinite loop.

Describe the proposed solution

A new flag on the event object (say, event.isSyntheticRequest) makes sense to me here.

Alternatives considered

For right now, my plan is to add a dummy x-is-ssr-api-fetch header to the appropriate requests as part of my handleFetch hook, and then in my handle hook look for that header, remove the header, and deal with the request appropriately.

Importance

would make my life easier

Additional Information

We already have event.isDataRequest. I'm not entirely thrilled about the word 'synthetic' here, but I do think some name of the form event.is*Request makes sense here.

Conduitry avatar Jun 15 '23 15:06 Conduitry

I was just taking a peek at what might be involved in implementing this, and I realized I wasn't clear on what the desired behavior was, exactly. Should the event that's passed to handleFetch also have isSyntheticRequest: true (or whatever we call it)? (How would we know whether a given request should be marked as such? Maybe that's an argument for not setting it on any of them.)

Or should respond() in respond.js take a new parameter/option (passed in to it by create_fetch() in fetch.js) that tells it when it creates its event object, it should set isSyntheticRequest: true on it? Or something else?

Conduitry avatar Jun 16 '23 04:06 Conduitry

Looking at this again, maybe we already have what we need with state.depth? Maybe respond() can just construct the event object with isSyntheticRequest: state.depth > 0?

Conduitry avatar Jun 16 '23 11:06 Conduitry