Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: respect trailing slash option when navigating from the root page of the basepath #11388

Merged
merged 5 commits into from
Jan 9, 2024
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/five-garlics-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: respect the trailing slash option when navigating from the basepath root page
20 changes: 12 additions & 8 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,19 @@ async function get_navigation_result_from_branch({
}) {
/** @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
url.search = url.search; // turn `/?` into `/`

Expand Down Expand Up @@ -694,12 +703,7 @@ async function load_node({ loader, parent, url, params, route, server_data_node
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
};
}

Expand Down
2 changes: 2 additions & 0 deletions packages/kit/test/apps/options-2/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

<p data-testid="base">base: {base}</p>
<p data-testid="assets">assets: {assets}</p>

<a href="{base}/hello" data-testid="link">Go to /hello</a>
15 changes: 15 additions & 0 deletions packages/kit/test/apps/options-2/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ test.describe('paths', () => {
expect(await page.textContent('[data-testid="base"]')).toBe(`base: ${base}`);
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
}) => {
await page.goto('/basepath');
expect(new URL(page.url()).pathname).toBe('/basepath/');
await clicknav('[data-testid="link"]');
expect(new URL(page.url()).pathname).toBe('/basepath/hello');
});
});

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