Repository navigation
Description
Describe the bug
I'm using a code-server cloud environment to develop a SvelteKit project. In order to access the dev server, a proxy is provided that uses a subpath to proxy the dev server port (see docs. This leads me to need a base path to be set for the dev server.
If I set kit.paths.base as stated in the documentation, the first page load works correctly but the import statements are relative to the root which returns a 404, correctly, because those files are not available without the proxy. If I run build and preview everything works as expected since the base path is built into the compiled project.
Expected behavior
All paths in the dev server reference the base path instead of being relative to the root domain.
Reproduction
- Create a new SvelteKit project
- Set
kit.paths.baseto a non-root path - Run the dev server
Expected result
All import statements look for paths with the base path or relative to the base path
Actual result
Module import statements are relative to the root
Logs
GEThttp://domain.tld/.svelte-kit/dev/runtime/internal/start.js
[HTTP/1.1 404 Not Found 8ms]
Loading module from βhttp://domain.tld/.svelte-kit/dev/runtime/internal/start.jsβ was blocked because of a disallowed MIME type (βapplication/jsonβ). 3000
Loading failed for the module with source βhttp://domain.tld/.svelte-kit/dev/runtime/internal/start.jsβ. 3000:13:1
GEThttp://domain.tld/favicon.png
[HTTP/1.1 404 Not Found 6ms]System Info
System:
OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
CPU: (12) x64 Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
Memory: 283.55 MB / 15.32 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 14.18.2 - /usr/bin/node
Yarn: 1.22.15 - /usr/bin/yarn
npm: 6.14.15 - /usr/bin/npm
npmPackages:
@sveltejs/kit: ^1.0.0-next.201 => 1.0.0-next.201
svelte: ^3.42.6 => 3.44.2Severity
annoyance
Additional Information
I've been using the absproxy method with code-server. This passes the full path with the /absproxy/3000 portion intact. This seems like the correct approach for usage with kit.paths.base. I think this method would work fine if the dev server accounted for it when building the pages.
However, there is also the proxy method. The proxy method strips of the proxy subpath before passing the request to the dev server. Thus, on the dev server, all the paths are relative to root. If there was a way to specify a subpath only for use on the client side, this may also be a reasonable solution.
I've made some inline edits to the @sveltejs/kit package in node_modules and was able to get the proxy method working. It would require a config field to set it, of course. Obviously, not everyone is working in a cloud server π


Activity
andycarlberg commentedon Dec 10, 2021
Decided to see if I could fix this myself. After digging in it looks like this might actually be a Vite issue, not a SvelteKit issue.
It's possible that SvelteKit may need additional updates beyond the fix for Vite as well.
Bastian commentedon Dec 14, 2021
I'm having the same issue. My dev server is running behind a Traefik Reverse Proxy and thus it fails to resolve the

.svelte-kit/*files which are ignoring thekit.paths.basesetting:This is the config with the
kit.paths.baseset to/demo:andycarlberg commentedon Dec 14, 2021
@Bastian is correct, this is not actually a Vite issue as I first suspected. It does seem to be specific to SvelteKit. So I'll keep looking but I'm probably out of my depth here for a quick fix.
I started looking at it as a Vite problem because I always seemed to come back to Vite when trying to trace the issue through SvelteKit. I must have missed something though since a vanilla Vite project applies the base path as expected.
rassie commentedon Dec 14, 2021
@andycarlberg
Could you share a gist of what you've changed?
andycarlberg commentedon Dec 14, 2021
@rassie
I've pushed up some of my work-in-progress tinkering with the change essentially here.
This applies the base path prefix to the entry file. This is not a complete fix because I'm having trouble finding where these paths should actually be set. It's not a complete solution, more proof that there's an issue to be addressed I think.
andycarlberg commentedon Dec 23, 2021
I feel like I'm on the right track here with this change.
It correctly outputs the file path with the base prefix but it 404's when looking for that
start.jsfile. In debugging, it seems like that file is treated differently than other requests since the SvelteKit middleware doesn't process that file without my change (i.e. root-relative).Any hints on where I might look to get it to recognize that file as the special start file even if it has the prefix?
bjaskj commentedon Jan 10, 2022
I spent some time on something I think is similar, but I did not manage to figure out a fix for it.
I'm trying to use svelte kit via proxy server, and it works fine with static build and serve via other folder than root (having
kit.paths.base) but it goes around and tries to include files that are in "root", like node modules etc. I only got it working if I set the base in vite config in the kit package and also modified the prefix, see below:I think it boils down to the core/dev/index.js having a hardcoded value of "/", I tried playing around with this with some success but I ran into other issues trying to find a way to set it.
I also looked into
prefixset to value of "" in this core/dev/plugin.jsThis makes the paths to the script resources etc used in start method inside runtime/server/page/render.js
Maybe someone can help with a fix on this?
jp-simons commentedon Jan 10, 2022
jp-simons commentedon Jan 13, 2022
bjaskj commentedon Jan 13, 2022
What happen to me was that if I set kit.paths.base and then also the vite base path, which is hardcoded to '/' in kit, then I have to use
/mypath/mypathto get to index. So something is a bit bork. Also if I managed to get it use/mypath, then all the other paths (node_modules, svelte kit etc) would still go to root/hostnamehttp://localhost/.svelte-kit.jp-simons commentedon Jan 14, 2022
benmccann commentedon Jan 20, 2022
49 remaining items
alexbjorlig commentedon Nov 20, 2022
I'm trying to figure out why I can't serve Sveltekit from a different directory - is this issue the culprit?
benmccann commentedon Dec 10, 2022
This is no longer blocked by Vite, but I believe an additional change will be needed on our side
lc-spxl commentedon Jan 12, 2023
Any update on this ?
duruer commentedon Jan 15, 2023
Yes, Vite fixed the issue, but seems like SvelteKit is still using
/node_modulespath.duruer commentedon Jan 15, 2023
Seems like one of the causing issue is this line:
https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/vite/dev/index.js#L96
If I change the line like this:
It kinda fixes the issue but, then when you visit
/test, it throws this error in console and shows 404 page in browser:benmccann commentedon Jan 21, 2023
Perhaps this line:
kit/packages/kit/src/runtime/server/page/render.js
Line 168 in 06a56ae
Needs to be changed to:
Would someone affected by this problem be able to test it out?
SudoerWithAnOpinion commentedon Jan 21, 2023
Looks like this change works, I'm using coder's code-server and serving from some-domain/proxy/5173
The page is loading the start script from the correct location.

janabimustafa commentedon Mar 9, 2023
The solution appears to be inadequate in handling cases where paths.relative is enabled but paths.base is absent, which was implemented to enable flexible base paths.
If a reverse-proxy is created and directed to the site using a path like
/site-pr-10, theapp.jsfile will load correctly but thestart.jsfile will not. Essentially, this solution disrupts the functionality described in issue #595 when used behind a reverse proxy.Configure project to use local vite, find answer