diff --git a/.changeset/gold-tips-cover.md b/.changeset/gold-tips-cover.md new file mode 100644 index 000000000000..a9c0a96e02ab --- /dev/null +++ b/.changeset/gold-tips-cover.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: prerender data when there is no server load but the `trailingSlash` option is set from the server diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js index a41aa93b2894..83f30cb29d71 100644 --- a/packages/kit/src/runtime/server/page/index.js +++ b/packages/kit/src/runtime/server/page/index.js @@ -70,7 +70,10 @@ export async function render_page(event, page, options, manifest, state, resolve } } - const should_prerender_data = nodes.some((node) => node?.server?.load); + const should_prerender_data = nodes.some( + // prerender in case of trailingSlash because the client retrieves that value from the server + (node) => node?.server?.load || node?.server?.trailingSlash !== undefined + ); const data_pathname = add_data_suffix(event.url.pathname); // it's crucial that we do this before returning the non-SSR response, otherwise diff --git a/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/+page.svelte b/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/+page.svelte new file mode 100644 index 000000000000..71d69f88341a --- /dev/null +++ b/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/+page.svelte @@ -0,0 +1,5 @@ + + +{base}/trailing-slash-server/prerender diff --git a/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/prerender/+page.server.js b/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/prerender/+page.server.js new file mode 100644 index 000000000000..69e0d18ba8ad --- /dev/null +++ b/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/prerender/+page.server.js @@ -0,0 +1,2 @@ +export const trailingSlash = 'always'; +export const prerender = true; diff --git a/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/prerender/+page.svelte b/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/prerender/+page.svelte new file mode 100644 index 000000000000..79966403d499 --- /dev/null +++ b/packages/kit/test/apps/options-2/src/routes/trailing-slash-server/prerender/+page.svelte @@ -0,0 +1,5 @@ + + +

{page.url.pathname}

diff --git a/packages/kit/test/apps/options-2/test/test.js b/packages/kit/test/apps/options-2/test/test.js index 5cfc8c8fed69..1c5c0ffd525a 100644 --- a/packages/kit/test/apps/options-2/test/test.js +++ b/packages/kit/test/apps/options-2/test/test.js @@ -55,6 +55,23 @@ test.describe('paths', () => { }); }); +test.describe('trailing slash', () => { + if (!process.env.DEV) { + test('trailing slash server prerendered without server load', async ({ + page, + clicknav, + javaScriptEnabled + }) => { + if (!javaScriptEnabled) return; + + await page.goto('/basepath/trailing-slash-server'); + + await clicknav('a[href="/basepath/trailing-slash-server/prerender"]'); + expect(await page.textContent('h2')).toBe('/basepath/trailing-slash-server/prerender/'); + }); + } +}); + test.describe('Service worker', () => { if (process.env.DEV) return;