Skip to content

Commit

Permalink
fix: correctly handle trailing slash redirect when navigating from ro…
Browse files Browse the repository at this point in the history
…ot page (#11357)
  • Loading branch information
eltigerchino authored Dec 17, 2023
1 parent 678b637 commit fa31ad1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
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

0 comments on commit fa31ad1

Please sign in to comment.