Skip to content

Commit 9556aba

Browse files
fix: respect trailing slash option when navigating from the root page of the basepath (#11388)
* add failing test * only resolve root page trailing slash during navigation * changeset * fix test --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent c7a195a commit 9556aba

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

.changeset/five-garlics-greet.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: respect the trailing slash option when navigating from the basepath root page

packages/kit/src/runtime/client/client.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,19 @@ async function get_navigation_result_from_branch({
441441
}) {
442442
/** @type {import('types').TrailingSlash} */
443443
let slash = 'never';
444-
for (const node of branch) {
445-
if (node?.slash !== undefined) slash = node.slash;
444+
445+
// if `paths.base === '/a/b/c`, then the root route is always `/a/b/c/`, regardless of
446+
// the `trailingSlash` route option, so that relative paths to JS and CSS work
447+
if (base && (url.pathname === base || url.pathname === base + '/')) {
448+
slash = 'always';
449+
} else {
450+
for (const node of branch) {
451+
if (node?.slash !== undefined) slash = node.slash;
452+
}
446453
}
454+
447455
url.pathname = normalize_path(url.pathname, slash);
456+
448457
// eslint-disable-next-line
449458
url.search = url.search; // turn `/?` into `/`
450459

@@ -694,12 +703,7 @@ async function load_node({ loader, parent, url, params, route, server_data_node
694703
server: server_data_node,
695704
universal: node.universal?.load ? { type: 'data', data, uses } : null,
696705
data: data ?? server_data_node?.data ?? null,
697-
// if `paths.base === '/a/b/c`, then the root route is always `/a/b/c/`, regardless of
698-
// the `trailingSlash` route option, so that relative paths to JS and CSS work
699-
slash:
700-
base && (url.pathname === base || url.pathname === base + '/')
701-
? 'always'
702-
: node.universal?.trailingSlash ?? server_data_node?.slash
706+
slash: node.universal?.trailingSlash ?? server_data_node?.slash
703707
};
704708
}
705709

packages/kit/test/apps/options-2/src/routes/+page.svelte

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66

77
<p data-testid="base">base: {base}</p>
88
<p data-testid="assets">assets: {assets}</p>
9+
10+
<a href="{base}/hello" data-testid="link">Go to /hello</a>

packages/kit/test/apps/options-2/test/test.js

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ test.describe('paths', () => {
3737
expect(await page.textContent('[data-testid="base"]')).toBe(`base: ${base}`);
3838
expect(await page.textContent('[data-testid="assets"]')).toBe(`assets: ${base}`);
3939
});
40+
41+
test('serves /basepath with trailing slash always', async ({ page }) => {
42+
await page.goto('/basepath');
43+
expect(new URL(page.url()).pathname).toBe('/basepath/');
44+
});
45+
46+
test('respects trailing slash option when navigating from /basepath', async ({
47+
page,
48+
clicknav
49+
}) => {
50+
await page.goto('/basepath');
51+
expect(new URL(page.url()).pathname).toBe('/basepath/');
52+
await clicknav('[data-testid="link"]');
53+
expect(new URL(page.url()).pathname).toBe('/basepath/hello');
54+
});
4055
});
4156

4257
test.describe('Service worker', () => {

0 commit comments

Comments
 (0)