Skip to content

Commit

Permalink
fix: prerender data when there's no server load but trailingSlash o…
Browse files Browse the repository at this point in the history
…ption is set from the server (#13262)

fixes #13255

trailingSlash is evaluated on the client by making a server call, so we need to prerender it
  • Loading branch information
eltigerchino authored Jan 6, 2025
1 parent 48d2aae commit 80a9e83
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/gold-tips-cover.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { base } from '$app/paths';
</script>

<a href="{base}/trailing-slash-server/prerender">{base}/trailing-slash-server/prerender</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const trailingSlash = 'always';
export const prerender = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { page } from '$app/state';
</script>

<h2>{page.url.pathname}</h2>
17 changes: 17 additions & 0 deletions packages/kit/test/apps/options-2/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 80a9e83

Please sign in to comment.