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: prevent incorrect trailing slash redirect for prerendered root page when paths.base is set #10763

Merged
merged 4 commits into from
Dec 13, 2023
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/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/`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an explanation of why that is? It seems quite surprising to me that we would ignore the trailing slash option for the root page when base is set. I remember putting a lot of effort into an earlier version of Vite to allow it to work without a trailing slash and I don't understand why we'd have this new behavior

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original explanation is in this previous PR #9343 However it was not applied in vite preview, hence this PR. But I think this got removed in 2.0 because vite always does it? I have to check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry, I didn't mean to comment on a closed PR. I was on mobile and thought I was looking at #11357

// 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 @@ -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
};
}

Expand Down