Skip to content

feat: event.isSubRequest #10170

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

Merged
merged 16 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-ears-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: add `event.isSubRequest` boolean indicating whether this is a call to one of the app's own APIs during SSR (or prerendering)
4 changes: 4 additions & 0 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,10 @@ export interface RequestEvent<
* related to the data request in this case. Use this property instead if the distinction is important to you.
*/
isDataRequest: boolean;
/**
* `true` for `+server.js` calls coming from SvelteKit without the overhead of actually making an HTTP request. This happens when you make same-origin `fetch` requests on the server.
*/
isSubRequest: boolean;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export async function respond(request, options, manifest, state) {
}
},
url,
isDataRequest: is_data_request
isDataRequest: is_data_request,
isSubRequest: state.depth > 0
};

/** @type {import('types').RequiredResolveOptions} */
Expand Down
9 changes: 9 additions & 0 deletions packages/kit/test/apps/basics/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ export const handle = sequence(
}
return resolve(event);
},
({ event, resolve }) => {
if (
event.request.headers.has('host') &&
!event.request.headers.has('user-agent') !== event.isSubRequest
) {
throw new Error('SSR API sub-requests should have isSubRequest set to true');
}
return resolve(event);
},
({ event, resolve }) => {
if (event.url.pathname.includes('fetch-credentialed')) {
// Only get the cookie at the test where we know it's set to avoid polluting our logs with (correct) warnings
Expand Down