From e13318243bde043f5d613ada5b4ae2fe281defea Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Tue, 19 Dec 2023 01:22:50 +0800 Subject: [PATCH 1/4] add failing test --- .../kit/test/apps/options-2/src/routes/+page.svelte | 2 ++ packages/kit/test/apps/options-2/test/test.js | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/packages/kit/test/apps/options-2/src/routes/+page.svelte b/packages/kit/test/apps/options-2/src/routes/+page.svelte index d6232afc0cc9..58c6a7a7a8c4 100644 --- a/packages/kit/test/apps/options-2/src/routes/+page.svelte +++ b/packages/kit/test/apps/options-2/src/routes/+page.svelte @@ -6,3 +6,5 @@

base: {base}

assets: {assets}

+ +Go to /hello diff --git a/packages/kit/test/apps/options-2/test/test.js b/packages/kit/test/apps/options-2/test/test.js index 7defc47a6804..6ddf32a8b02e 100644 --- a/packages/kit/test/apps/options-2/test/test.js +++ b/packages/kit/test/apps/options-2/test/test.js @@ -37,6 +37,16 @@ test.describe('paths', () => { expect(await page.textContent('[data-testid="base"]')).toBe(`base: ${base}`); expect(await page.textContent('[data-testid="assets"]')).toBe(`assets: ${base}`); }); + + test('respects trailing slash option when navigating from /basepath', async ({ + page, + clicknav + }) => { + await page.goto('/basepath'); + expect(new URL(page.url()).pathname).toBe('/basepath/'); + await clicknav('[href="/basepath/hello"]'); + expect(new URL(page.url()).pathname).toBe('/basepath/hello'); + }); }); test.describe('Service worker', () => { From 586e965e7b3472925595c04001e1310866462b65 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Tue, 19 Dec 2023 01:28:01 +0800 Subject: [PATCH 2/4] only resolve root page trailing slash during navigation --- packages/kit/src/runtime/client/client.js | 17 +++++++++-------- packages/kit/test/apps/options-2/test/test.js | 5 +++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 80fa5c07e116..195d132960ac 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -407,8 +407,14 @@ export function create_client(app, target) { }) { /** @type {import('types').TrailingSlash} */ let slash = 'never'; - for (const node of branch) { - if (node?.slash !== undefined) slash = node.slash; + // 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 + if (base && (url.pathname === base || url.pathname === base + '/')) { + slash = 'always'; + } else { + for (const node of branch) { + if (node?.slash !== undefined) slash = node.slash; + } } url.pathname = normalize_path(url.pathname, slash); // eslint-disable-next-line @@ -660,12 +666,7 @@ 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 always `/a/b/c/`, regardless of - // the `trailingSlash` route option, so that relative paths to JS and CSS work - slash: - base && (url.pathname === base || url.pathname === base + '/') - ? 'always' - : node.universal?.trailingSlash ?? server_data_node?.slash + slash: node.universal?.trailingSlash ?? server_data_node?.slash }; } diff --git a/packages/kit/test/apps/options-2/test/test.js b/packages/kit/test/apps/options-2/test/test.js index 6ddf32a8b02e..176257c19bab 100644 --- a/packages/kit/test/apps/options-2/test/test.js +++ b/packages/kit/test/apps/options-2/test/test.js @@ -38,6 +38,11 @@ test.describe('paths', () => { expect(await page.textContent('[data-testid="assets"]')).toBe(`assets: ${base}`); }); + test('serves /basepath with trailing slash always', async ({ page }) => { + await page.goto('/basepath'); + expect(new URL(page.url()).pathname).toBe('/basepath/'); + }); + test('respects trailing slash option when navigating from /basepath', async ({ page, clicknav From 01871a31106ff372c47b0a2cc604ba180e917616 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Tue, 19 Dec 2023 01:33:09 +0800 Subject: [PATCH 3/4] changeset --- .changeset/five-garlics-greet.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/five-garlics-greet.md diff --git a/.changeset/five-garlics-greet.md b/.changeset/five-garlics-greet.md new file mode 100644 index 000000000000..8b1e1b123639 --- /dev/null +++ b/.changeset/five-garlics-greet.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +fix: respect the trailing slash option when navigating from the basepath root page From b661d9edaa4e34ed6f4442ae0d6a2cfd3b538897 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Tue, 19 Dec 2023 09:09:11 +0800 Subject: [PATCH 4/4] fix test --- packages/kit/test/apps/options-2/src/routes/+page.svelte | 2 +- packages/kit/test/apps/options-2/test/test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/test/apps/options-2/src/routes/+page.svelte b/packages/kit/test/apps/options-2/src/routes/+page.svelte index 58c6a7a7a8c4..badacc8173d8 100644 --- a/packages/kit/test/apps/options-2/src/routes/+page.svelte +++ b/packages/kit/test/apps/options-2/src/routes/+page.svelte @@ -7,4 +7,4 @@

base: {base}

assets: {assets}

-Go to /hello +Go to /hello diff --git a/packages/kit/test/apps/options-2/test/test.js b/packages/kit/test/apps/options-2/test/test.js index 176257c19bab..3f9514de8799 100644 --- a/packages/kit/test/apps/options-2/test/test.js +++ b/packages/kit/test/apps/options-2/test/test.js @@ -49,7 +49,7 @@ test.describe('paths', () => { }) => { await page.goto('/basepath'); expect(new URL(page.url()).pathname).toBe('/basepath/'); - await clicknav('[href="/basepath/hello"]'); + await clicknav('[data-testid="link"]'); expect(new URL(page.url()).pathname).toBe('/basepath/hello'); }); });