Skip to content

Commit

Permalink
fix: prevent incorrect trailing slash redirect for prerendered root p…
Browse files Browse the repository at this point in the history
…age when `paths.base` is set (#10763)

fixes #9982
  • Loading branch information
eltigerchino committed Dec 13, 2023
1 parent f7b4f18 commit 5b1d1ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-pandas-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: prerendered root page with `paths.base` config uses correct trailing slash option
14 changes: 13 additions & 1 deletion packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,19 @@ export async function preview(vite, vite_config, svelte_config) {

vite.middlewares.use((req, res, next) => {
const original_url = /** @type {string} */ (req.url);
const { pathname } = new URL(original_url, 'http://dummy');
const { pathname, search } = new URL(original_url, 'http://dummy');

// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
// regardless of the `trailingSlash` route option
if (base.length > 1 && pathname === base) {
let location = base + '/';
if (search) location += search;
res.writeHead(307, {
location
});
res.end();
return;
}

if (pathname.startsWith(base)) {
next();
Expand Down
7 changes: 6 additions & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,12 @@ 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,
slash: node.universal?.trailingSlash ?? server_data_node?.slash
// if `paths.base === '/a/b/c`, then the root route is `/a/b/c/`,
// regardless of the `trailingSlash` route option
slash:
url.pathname === base || url.pathname === base + '/'
? 'always'
: node.universal?.trailingSlash ?? server_data_node?.slash
};
}

Expand Down

0 comments on commit 5b1d1ab

Please sign in to comment.