Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-plums-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: correctly handle trailing slash redirect when navigating from the root page
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,10 @@ export function create_client(app, target) {
server: server_data_node,
universal: node.universal?.load ? { type: 'data', data, uses } : null,
data: data ?? server_data_node?.data ?? null,
// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
// regardless of the `trailingSlash` route option
// if `paths.base === '/a/b/c`, then the root route is always `/a/b/c/`, regardless of
// the `trailingSlash` route option, so that relative paths to JS and CSS work
slash:
url.pathname === base || url.pathname === base + '/'
base && (url.pathname === base || url.pathname === base + '/')
? 'always'
: node.universal?.trailingSlash ?? server_data_node?.slash
};
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/test/apps/basics/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
</script>

<h1>the answer is {data.answer}</h1>

<a href="/routing/trailing-slash/never/"
>URL with trailing slash that should redirect to remove it</a
>
10 changes: 10 additions & 0 deletions packages/kit/test/apps/basics/test/cross-platform/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,16 @@ test.describe('Routing', () => {
expect(new URL(page.url()).pathname).toBe('/routing/trailing-slash/ignore/');
await expect(page.locator('p')).toHaveText('/routing/trailing-slash/ignore/');
});

test('trailing slash redirect works when navigating from root page', async ({
page,
clicknav
}) => {
await page.goto('/');
await clicknav('a[href="/routing/trailing-slash/never/"]');
expect(new URL(page.url()).pathname).toBe('/routing/trailing-slash/never');
await expect(page.locator('p')).toHaveText('/routing/trailing-slash/never');
});
});

test.describe('Shadow DOM', () => {
Expand Down