diff --git a/.changeset/angry-ways-divide.md b/.changeset/angry-ways-divide.md new file mode 100644 index 000000000000..0ce5f097e568 --- /dev/null +++ b/.changeset/angry-ways-divide.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +[fix] prerendered endpoint callable from non-prerendered server load diff --git a/packages/kit/src/runtime/server/data/index.js b/packages/kit/src/runtime/server/data/index.js index c8d63169de14..eec34a4598b7 100644 --- a/packages/kit/src/runtime/server/data/index.js +++ b/packages/kit/src/runtime/server/data/index.js @@ -31,6 +31,8 @@ export async function render_data( }); } + state.initiator = route; + try { const node_ids = [...route.page.layouts, route.page.leaf]; const invalidated = invalidated_data_nodes ?? node_ids.map(() => true); diff --git a/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/api/+server.js b/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/api/+server.js new file mode 100644 index 000000000000..3d76a1f5de22 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/api/+server.js @@ -0,0 +1,7 @@ +import { json } from '@sveltejs/kit'; + +export const prerender = true; + +export function GET() { + return json({ message: 'Im prerendered and called from a non-prerendered +page.server.js' }); +} diff --git a/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/page/+page.server.js b/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/page/+page.server.js new file mode 100644 index 000000000000..4c5ccfc08306 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/page/+page.server.js @@ -0,0 +1,6 @@ +/** @type {import('./$types').PageServerLoad} */ +export async function load({ fetch }) { + const res = await fetch('/prerendering/prerendered-endpoint/api'); + const json = await res.json(); + return json; +} diff --git a/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/page/+page.svelte b/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/page/+page.svelte new file mode 100644 index 000000000000..439e64448c2b --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/prerendering/prerendered-endpoint/page/+page.svelte @@ -0,0 +1,5 @@ + + +

{data.message}

diff --git a/packages/kit/test/apps/basics/test/test.js b/packages/kit/test/apps/basics/test/test.js index b6a8c844c10b..e7d05e552950 100644 --- a/packages/kit/test/apps/basics/test/test.js +++ b/packages/kit/test/apps/basics/test/test.js @@ -940,6 +940,23 @@ test.describe('Load', () => { expect(await page.textContent('h1')).toBe('true'); }); + + test('Prerendered +server.js called from a non-prerendered +page.server.js works', async ({ + page, + app, + javaScriptEnabled + }) => { + if (javaScriptEnabled) { + await page.goto('/'); + await app.goto('/prerendering/prerendered-endpoint/page'); + } else { + await page.goto('/prerendering/prerendered-endpoint/page'); + } + + expect(await page.textContent('h1')).toBe( + 'Im prerendered and called from a non-prerendered +page.server.js' + ); + }); }); test.describe('Nested layouts', () => {