Skip to content

Commit 5b1d1ab

Browse files
authored
fix: prevent incorrect trailing slash redirect for prerendered root page when paths.base is set (#10763)
fixes #9982
1 parent f7b4f18 commit 5b1d1ab

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

.changeset/rich-pandas-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: prerendered root page with `paths.base` config uses correct trailing slash option

packages/kit/src/exports/vite/preview/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,19 @@ export async function preview(vite, vite_config, svelte_config) {
7171

7272
vite.middlewares.use((req, res, next) => {
7373
const original_url = /** @type {string} */ (req.url);
74-
const { pathname } = new URL(original_url, 'http://dummy');
74+
const { pathname, search } = new URL(original_url, 'http://dummy');
75+
76+
// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
77+
// regardless of the `trailingSlash` route option
78+
if (base.length > 1 && pathname === base) {
79+
let location = base + '/';
80+
if (search) location += search;
81+
res.writeHead(307, {
82+
location
83+
});
84+
res.end();
85+
return;
86+
}
7587

7688
if (pathname.startsWith(base)) {
7789
next();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,12 @@ export function create_client(app, target) {
572572
server: server_data_node,
573573
universal: node.universal?.load ? { type: 'data', data, uses } : null,
574574
data: data ?? server_data_node?.data ?? null,
575-
slash: node.universal?.trailingSlash ?? server_data_node?.slash
575+
// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
576+
// regardless of the `trailingSlash` route option
577+
slash:
578+
url.pathname === base || url.pathname === base + '/'
579+
? 'always'
580+
: node.universal?.trailingSlash ?? server_data_node?.slash
576581
};
577582
}
578583

0 commit comments

Comments
 (0)