Skip to content

Commit

Permalink
feat: add event.isSubRequest (#10170)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Jun 28, 2023
1 parent 67c0214 commit 4069622
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
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

0 comments on commit 4069622

Please sign in to comment.