Skip to content

Commit 64a279f

Browse files
authored
fix: initialise state.initiator for render_endpoint() (#8869)
ensure endpoints can fetch endpoints on the same host but not part of the application fixes #8851
1 parent 02bd767 commit 64a279f

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

.changeset/slimy-donkeys-itch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: ensure endpoints can fetch endpoints on the same host but not part of the application

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { method_not_allowed } from './utils.js';
44

55
/**
66
* @param {import('types').RequestEvent} event
7+
* @param {import('types').SSRRoute} route
78
* @param {import('types').SSREndpoint} mod
89
* @param {import('types').SSRState} state
910
* @returns {Promise<Response>}
1011
*/
11-
export async function render_endpoint(event, mod, state) {
12+
export async function render_endpoint(event, route, mod, state) {
1213
const method = /** @type {import('types').HttpMethod} */ (event.request.method);
1314

1415
let handler = mod[method];
@@ -38,6 +39,8 @@ export async function render_endpoint(event, mod, state) {
3839
}
3940
}
4041

42+
state.initiator = route;
43+
4144
try {
4245
const response = await handler(
4346
/** @type {import('types').RequestEvent<Record<string, any>>} */ (event)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ export async function respond(request, options, manifest, state) {
358358
trailing_slash ?? 'never'
359359
);
360360
} else if (route.endpoint && (!route.page || is_endpoint_request(event))) {
361-
response = await render_endpoint(event, await route.endpoint(), state);
361+
response = await render_endpoint(event, route, await route.endpoint(), state);
362362
} else if (route.page) {
363363
response = await render_page(
364364
event,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function GET({ fetch }) {
2+
return await fetch('/prerendering/prerendered-endpoint/api');
3+
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ test.describe('Endpoints', () => {
9797
expect(headers.head).toEqual(headers.get);
9898
});
9999

100+
test('Prerendered +server.js called from a non-prerendered +server.js works', async ({
101+
baseURL
102+
}) => {
103+
const res = await fetch(`${baseURL}/prerendering/prerendered-endpoint/proxy`);
104+
105+
expect(res.status).toBe(200);
106+
expect(await res.json()).toStrictEqual({
107+
message: 'Im prerendered and called from a non-prerendered +page.server.js'
108+
});
109+
});
110+
100111
// TODO all the remaining tests in this section are really only testing
101112
// setResponse, since we're not otherwise changing anything on the response.
102113
// might be worth making these unit tests instead

0 commit comments

Comments
 (0)