Skip to content

Dev server does not apply base path correctlyΒ #2958

Description

@andycarlberg

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

  1. Create a new SvelteKit project
  2. Set kit.paths.base to a non-root path
  3. 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

Example
image
image

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.2

Severity

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

  1. andycarlberg commented on Dec 10, 2021

    @andycarlberg
    Author

    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.

  2. Bastian commented on Dec 14, 2021

    @Bastian

    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 the kit.paths.base setting:
    image

    This is the config with the kit.paths.base set to /demo:

    import adapter from '@sveltejs/adapter-node';
    import preprocess from 'svelte-preprocess';
    
    /** @type {import('@sveltejs/kit').Config} */
    const config = {
    	// Consult https://github.com/sveltejs/svelte-preprocess
    	// for more information about preprocessors
    	preprocess: [
    		preprocess({
    			postcss: true
    		})
    	],
    
    	kit: {
    		adapter: adapter(),
    
    		// hydrate the <div id="svelte"> element in src/app.html
    		target: '#svelte',
    		ssr: true,
    		trailingSlash: 'ignore',
    		paths: {
    			base: '/demo'
    		}
    	}
    };
    
    export default config;
  3. andycarlberg commented on Dec 14, 2021

    @andycarlberg
    Author

    @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.

  4. rassie commented on Dec 14, 2021

    @rassie

    @andycarlberg

    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 smile

    Could you share a gist of what you've changed?

  5. andycarlberg commented on Dec 14, 2021

    @andycarlberg
    Author

    @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.

  6. andycarlberg commented on Dec 23, 2021

    @andycarlberg
    Author

    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.js file. 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?

  7. bjaskj commented on Jan 10, 2022

    @bjaskj

    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 prefix set to value of "" in this core/dev/plugin.js

    This 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?

  8. jp-simons commented on Jan 10, 2022

    @jp-simons
  9. jp-simons commented on Jan 13, 2022

    @jp-simons
  10. bjaskj commented on Jan 13, 2022

    @bjaskj

    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/mypath to 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/hostname http://localhost/.svelte-kit.

  11. jp-simons commented on Jan 14, 2022

    @jp-simons
  12. added
    bug
    on Jan 18, 2022
  13. added this to the 1.0 milestone on Jan 18, 2022
  14. benmccann commented on Jan 20, 2022

    @benmccann
  15. 49 remaining items

  16. alexbjorlig commented on Nov 20, 2022

    @alexbjorlig
    Contributor

    I'm trying to figure out why I can't serve Sveltekit from a different directory - is this issue the culprit?

  17. benmccann commented on Dec 10, 2022

    @benmccann
    Member

    This is no longer blocked by Vite, but I believe an additional change will be needed on our side

  18. lc-spxl commented on Jan 12, 2023

    @lc-spxl

    Any update on this ?

  19. duruer commented on Jan 15, 2023

    @duruer

    This is no longer blocked by Vite, but I believe an additional change will be needed on our side

    Yes, Vite fixed the issue, but seems like SvelteKit is still using /node_modules path.

  20. duruer commented on Jan 15, 2023

    @duruer

    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:

    file: `/test${runtime_base}/client/start.js`,
    

    It kinda fixes the issue but, then when you visit /test, it throws this error in console and shows 404 page in browser:

    TypeError: Cannot create proxy with a non-object as target or handler
        at Module.handle_error_and_jsonify (/node_modules/@sveltejs/kit/src/runtime/server/utils.js:123:12)
        at Module.render_page (/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:251:25)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async resolve (/node_modules/@sveltejs/kit/src/runtime/server/respond.js:358:17)
        at async Module.respond (/node_modules/@sveltejs/kit/src/runtime/server/respond.js:229:20)
        at async file:///home/kahverengi/Projects/Pano/panel-ui/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:468:22
    
  21. benmccann commented on Jan 21, 2023

    @benmccann
    Member

    Perhaps this line:

    const prefixed = (path) => (path.startsWith('/') ? path : `${resolved_assets}/${path}`);

    Needs to be changed to:

    const prefixed = (path) => (path.startsWith('/') ? base + path : `${resolved_assets}/${path}`);

    Would someone affected by this problem be able to test it out?

  22. SudoerWithAnOpinion commented on Jan 21, 2023

    @SudoerWithAnOpinion

    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.
    image

  23. janabimustafa commented on Mar 9, 2023

    @janabimustafa
    Contributor

    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, the app.js file will load correctly but the start.js file will not. Essentially, this solution disrupts the functionality described in issue #595 when used behind a reverse proxy.

  24. removed
    bug
    on Aug 4, 2026
  25. added theissue type on Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p1-importantpaths.base

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions