diff --git a/.changeset/rich-pandas-provide.md b/.changeset/rich-pandas-provide.md new file mode 100644 index 000000000000..4b2ca5c9ec39 --- /dev/null +++ b/.changeset/rich-pandas-provide.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: prerendered root page with `paths.base` config uses correct trailing slash option diff --git a/packages/kit/src/exports/vite/preview/index.js b/packages/kit/src/exports/vite/preview/index.js index 77ac50745158..f535968b5c46 100644 --- a/packages/kit/src/exports/vite/preview/index.js +++ b/packages/kit/src/exports/vite/preview/index.js @@ -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(); diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 08465d6b5342..f434ac2398c5 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -568,7 +568,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 }; }