Description
Describe the bug
I need to integrate a SvelteKit app into an existing Express.js server at a dynamic base path. Based on #595 (comment) I assume this is intended to be supported through the kit.paths.relative
option, but trailing slash normalization seems to assume SvelteKit is top dog and redirects against the root.
Standard static paths.base
behavior (repro branch)
With a configuration like this:
const config = {
kit: {
adapter: adapterNode(),
paths: {
base: '/sveltekit',
relative: true
}
}
}
...and integration into a custom server like this:
app.use(handler);
/sveltekit/sverdle/
is correctly redirected to /sveltekit/sverdle
.
Dynamic paths.base
attempt (repro branch)
With a configuration like this:
const config = {
kit: {
adapter: adapterNode(),
paths: {
relative: true
}
}
};
...and integration like this:
app.use('/sveltekit', handler);
/sveltekit/sverdle/
is incorrectly redirected to /sverdle
.
Reproduction
HoldYourWaffle/sveltekit-express-dynamic-basepath, specifically repro--static-base
and repro--dynamic-base
.
System Info
System:
OS: Windows 10 10.0.19045
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 16.98 GB / 31.81 GB
Binaries:
Node: 22.14.0 - C:\Program Files\nodejs\node.EXE
npm: 10.9.2 - C:\Program Files\nodejs\npm.CMD
pnpm: 10.6.2 - ~\AppData\Local\pnpm\pnpm.CMD
Browsers:
Edge: Chromium (133.0.3065.69)
Internet Explorer: 11.0.19041.4355
Severity
blocking all usage of SvelteKit
Additional Information
The Location
header is allowed to be relative (MDN), so I think this should be an easy fix?™
Already working on a PR :)
Activity
[-]`trailingSlash` redirects against root instead of dynamic base[/-][+]`trailingSlash` always redirects against root[/+][-]`trailingSlash` always redirects against root[/-][+]Trailing slash normalization always redirects against root[/+]eltigerchino commentedon Apr 18, 2025
I think the issue is that Express strips the mount path from the request URL, so if you have
app.use('/sveltekit', handler)
and a user navigates to/sveltekit/about
, SvelteKit receives a different URL/about
. While some routing features work, it breaks features such as redirects, links, URL information, etc. #7242 (comment) sums this up welladapter-node
base path configurable via environment variable #7242HoldYourWaffle commentedon Apr 20, 2025
Just to double check - are you sure you didn't mix this up with #13702? I agree that both are strongly related to #7242, but this specific problem has a much simpler solution (#13719).
eltigerchino commentedon Apr 21, 2025
Sorry, after thinking about it more, the fix you've proposed should work even if the URL passed to SvelteKit is missing the base path. For example, if the requested path is
/base-path/about
and SvelteKit receives/about
, it will result in the same relative redirect such as../about/
. While this doesn't fix all the base path issues that a fix for #7242 would, I think it could be useful to have these relative redirects.