Skip to content

Commit

Permalink
fix: initialise state.initiator for render_endpoint() (#8869)
Browse files Browse the repository at this point in the history
ensure endpoints can fetch endpoints on the same host but not part of the application
fixes #8851
  • Loading branch information
eltigerchino authored Feb 3, 2023
1 parent 02bd767 commit 64a279f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-donkeys-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: ensure endpoints can fetch endpoints on the same host but not part of the application
5 changes: 4 additions & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { method_not_allowed } from './utils.js';

/**
* @param {import('types').RequestEvent} event
* @param {import('types').SSRRoute} route
* @param {import('types').SSREndpoint} mod
* @param {import('types').SSRState} state
* @returns {Promise<Response>}
*/
export async function render_endpoint(event, mod, state) {
export async function render_endpoint(event, route, mod, state) {
const method = /** @type {import('types').HttpMethod} */ (event.request.method);

let handler = mod[method];
Expand Down Expand Up @@ -38,6 +39,8 @@ export async function render_endpoint(event, mod, state) {
}
}

state.initiator = route;

try {
const response = await handler(
/** @type {import('types').RequestEvent<Record<string, any>>} */ (event)
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export async function respond(request, options, manifest, state) {
trailing_slash ?? 'never'
);
} else if (route.endpoint && (!route.page || is_endpoint_request(event))) {
response = await render_endpoint(event, await route.endpoint(), state);
response = await render_endpoint(event, route, await route.endpoint(), state);
} else if (route.page) {
response = await render_page(
event,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function GET({ fetch }) {
return await fetch('/prerendering/prerendered-endpoint/api');
}
11 changes: 11 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ test.describe('Endpoints', () => {
expect(headers.head).toEqual(headers.get);
});

test('Prerendered +server.js called from a non-prerendered +server.js works', async ({
baseURL
}) => {
const res = await fetch(`${baseURL}/prerendering/prerendered-endpoint/proxy`);

expect(res.status).toBe(200);
expect(await res.json()).toStrictEqual({
message: 'Im prerendered and called from a non-prerendered +page.server.js'
});
});

// TODO all the remaining tests in this section are really only testing
// setResponse, since we're not otherwise changing anything on the response.
// might be worth making these unit tests instead
Expand Down

0 comments on commit 64a279f

Please sign in to comment.