Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly trailing slash redirect when navigating from root page #11357

Merged
merged 7 commits into from
Dec 17, 2023
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 @@ -417,7 +417,7 @@
await expect(page.locator('input')).toBeFocused();
});

test('scroll positions are recovered on reloading the page', async ({ page, app }) => {

Check warning on line 420 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / Cross-browser-test (18, macOS-latest, webkit, dev)

flaky test: scroll positions are recovered on reloading the page

retries: 2
await page.goto('/anchor');
await page.evaluate(() => window.scrollTo(0, 1000));
await app.goto('/anchor/anchor');
Expand Down Expand Up @@ -742,6 +742,16 @@
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
Loading