Skip to content

Commit 4069622

Browse files
authored
feat: add event.isSubRequest (#10170)
1 parent 67c0214 commit 4069622

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

.changeset/small-ears-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: add `event.isSubRequest` boolean indicating whether this is a call to one of the app's own APIs during SSR (or prerendering)

packages/kit/src/exports/public.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,10 @@ export interface RequestEvent<
10171017
* related to the data request in this case. Use this property instead if the distinction is important to you.
10181018
*/
10191019
isDataRequest: boolean;
1020+
/**
1021+
* `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.
1022+
*/
1023+
isSubRequest: boolean;
10201024
}
10211025

10221026
/**

packages/kit/src/runtime/server/respond.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ export async function respond(request, options, manifest, state) {
169169
}
170170
},
171171
url,
172-
isDataRequest: is_data_request
172+
isDataRequest: is_data_request,
173+
isSubRequest: state.depth > 0
173174
};
174175

175176
/** @type {import('types').RequiredResolveOptions} */

packages/kit/test/apps/basics/src/hooks.server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ export const handle = sequence(
5656
}
5757
return resolve(event);
5858
},
59+
({ event, resolve }) => {
60+
if (
61+
event.request.headers.has('host') &&
62+
!event.request.headers.has('user-agent') !== event.isSubRequest
63+
) {
64+
throw new Error('SSR API sub-requests should have isSubRequest set to true');
65+
}
66+
return resolve(event);
67+
},
5968
({ event, resolve }) => {
6069
if (event.url.pathname.includes('fetch-credentialed')) {
6170
// Only get the cookie at the test where we know it's set to avoid polluting our logs with (correct) warnings

0 commit comments

Comments
 (0)