Skip to content

Commit

Permalink
fix: client-side trailing slash redirect when preloading data (#8982)
Browse files Browse the repository at this point in the history
* add client-side trailing slash redirect

* lint

* edit comment

* add comments
  • Loading branch information
eltigerchino authored Feb 10, 2023
1 parent 262b01b commit bfa2b6e
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shiny-eagles-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: client-side trailing slash redirect when preloading data
11 changes: 10 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export function create_client({ target }) {
);
return false;
}
} else if (/** @type {number} */ (navigation_result.props?.page?.status) >= 400) {
} else if (/** @type {number} */ (navigation_result.props.page?.status) >= 400) {
const updated = await stores.updated.check();
if (updated) {
await native_navigation(url);
Expand All @@ -331,6 +331,14 @@ export function create_client({ target }) {
capture_snapshot(previous_history_index);
}

// ensure the url pathname matches the page's trailing slash option
if (
navigation_result.props.page?.url &&
navigation_result.props.page.url.pathname !== url.pathname
) {
url.pathname = navigation_result.props.page?.url.pathname;
}

if (opts && opts.details) {
const { details } = opts;
const change = details.replaceState ? 0 : 1;
Expand All @@ -355,6 +363,7 @@ export function create_client({ target }) {
if (started) {
current = navigation_result.state;

// reset url before updating page store
if (navigation_result.props.page) {
navigation_result.props.page.url = url;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ssr = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { page } from '$app/stores';
</script>

<ul data-sveltekit-preload-data="hover">
<li><a href="/routing/trailing-slash/always">/always</a></li>
<li><a href="/routing/trailing-slash/ignore/">/ignore/</a></li>
<li><a href="/routing/trailing-slash/never/">/never/</a></li>
</ul>

<p>{$page.url.pathname}</p>

<slot />
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const trailingSlash = 'always';
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const trailingSlash = 'ignore';
Empty file.
Empty file.
16 changes: 16 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 @@ -660,6 +660,22 @@ test.describe('Routing', () => {
await page.locator(selector).click();
expect(await page.textContent(selector)).toBe('count: 1');
});

test('trailing slash redirect', async ({ page, clicknav }) => {
await page.goto('/routing/trailing-slash');

await clicknav('a[href="/routing/trailing-slash/always"]');
expect(new URL(page.url()).pathname).toBe('/routing/trailing-slash/always/');
await expect(page.locator('p')).toHaveText('/routing/trailing-slash/always/');

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');

await clicknav('a[href="/routing/trailing-slash/ignore/"]');
expect(new URL(page.url()).pathname).toBe('/routing/trailing-slash/ignore/');
await expect(page.locator('p')).toHaveText('/routing/trailing-slash/ignore/');
});
});

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

0 comments on commit bfa2b6e

Please sign in to comment.