kit icon indicating copy to clipboard operation
kit copied to clipboard

Dev server does not apply base path correctly

Open andycarlberg opened this issue 3 years ago • 54 comments

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 😄

andycarlberg avatar Dec 01 '21 17:12 andycarlberg

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.

andycarlberg avatar Dec 10 '21 19:12 andycarlberg

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;

Bastian avatar Dec 14 '21 11:12 Bastian

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

andycarlberg avatar Dec 14 '21 14:12 andycarlberg

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

rassie avatar Dec 14 '21 14:12 rassie

@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 avatar Dec 14 '21 15:12 andycarlberg

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?

andycarlberg avatar Dec 23 '21 22:12 andycarlberg

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?

bjaskj avatar Jan 10 '22 14:01 bjaskj

Not sure if this stackoverflow question is related: https://stackoverflow.com/questions/69378392/sveltekit-base-url-for-subdirectory-throws-404 They don't seem to be using a proxy though. I am using a proxy with stunnel and running into this issue, just working around with conditional svelteconfig, like:

{
  kit: {
    paths: {
      base: process.env.NODE_ENV === 'development' ? undefined : '/mybase',
    },
  },
}

Edit: The downside of this though is navigation, if I have a header on various pages at different levels in the tree, and it has links or "goto" calls, it'll need an absolute path which can't work in both prod and dev.

johnnysprinkles avatar Jan 10 '22 21:01 johnnysprinkles

So it turns out this is only the index route that fails to load, any deeper path works fine.

johnnysprinkles avatar Jan 13 '22 06:01 johnnysprinkles

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.

bjaskj avatar Jan 13 '22 08:01 bjaskj

So I just wanted to clarify and provide simple repo steps, and make sure I'm actually in the right bug (I can open a new one if not.) This doesn't have anything to do with proxies or missing js/css assets, it's actually that the main HTML document is 404 when using a base path and running in dev mode.

  1. Init a brand new SvelteKit app with npm init svelte@next sv which gives as of now, SvelteKit v1.0.0-next.230
  2. Choose the Demo App
  3. cd sv; npm i
  4. npm run dev
  5. Everything works fine
  6. Kill that, edit svelte.config.js to include kit.paths.base = '/foo'
  7. Start up again with npm run dev
  8. Note that both http://localhost:3000 and http://localhost:3000/foo give you a 404.
  9. Note that deeper paths do work however, i.e. http://localhost:3000/foo/about

johnnysprinkles avatar Jan 14 '22 22:01 johnnysprinkles

Note that both http://localhost:3000 and http://localhost:3000/foo give you a 404.

http://localhost:3000/foo works for me. Closing as fixed

benmccann avatar Jan 20 '22 21:01 benmccann

@benmccann pardon me asking, maybe I'm missing something here, but what exactly has been fixed? You basically just said "works for me" (which is fine and probably true for the example above), but has there been any change addressing the original problem and the comments above?

rassie avatar Jan 20 '22 21:01 rassie

I would also like to see this fixed, I can't use a dev env for some projects because of this issue.

bjaskj avatar Jan 20 '22 21:01 bjaskj

Thanks @benmccann I just tried with SvelteKit v1.0.0-next.236 and confirmed it's working. And to make sure I wasn't just doing something wrong before, I downgraded sveltekit to next.230 and observed the 404 again, so it seems to have been fixed.

There is a minor issue where the Demo app isn't prepending its static asset paths with $app/paths.base, so there's some broken images. I might file a separate super low priority bug for that.

It looks like I did hijack OPs original issue, sorry about that. Reading through it sounds like they want to slightly expand the meaning of kit.paths.base to skip over the base path in all contexts, even in local development. Can we re-open this bug? Even if not targeted for 1.0 it seems like a shame to make people fork one-liner edits to unblock themselves.

johnnysprinkles avatar Jan 20 '22 23:01 johnnysprinkles

Also, sorry @benmccann if I was kind of a jerk last year, I was pulling my hair out trying to make SvelteKit a workable option for a very large project at Google, but that never did go anywhere and I ended up quitting Google. Onward!

johnnysprinkles avatar Jan 20 '22 23:01 johnnysprinkles

@johnnysprinkles sorry to be blunt, and I don't think you intended this, but I think you derailed this issue for an unrelated problem.

@benmccann the problem in question still exists and it's a different one. In the example above, localhost:3000/foo is working, but the reason why it's working is because localhost:3000/.svelte-kit is still available without a proxy. If you put a proxy in front and only forward /foo to the server, you'd only forward requests to /foo, but not /.svelte-kit, you'll be getting 404 errors for SvelteKit runtime and nothing will work at all. The issue here is that kit.paths.base should be applied to /.svelte-kit too, which it's currently not.

You can easily verify it with the repro above, but look into Developer Tools, where you should see something like this:

image

If /.svelte-kit is not properly forwarded as /foo/.svelte-kit, nothing will work. And forwarding just /.svelte-kit is not a solution, since the whole reason people use proxies is to host multiple sites on the same domain, so most people need a /foo with /foo/.svelte-kit and a /bar with /bar/.svelte-kit.

rassie avatar Jan 21 '22 10:01 rassie

As OP, i wanted to verify that @rassie is correct. Their comment shows the issue I was originally trying to deal with. I haven't personally made any further progress on it but would agree the issue is not fixed.

andycarlberg avatar Jan 21 '22 12:01 andycarlberg

I confirm this issue, I was meant to user sveltekit on a /blog subdirectory, however all assets still load from root, when it should rewrite to kit.paths.base. Unfortunately will have to stick with nextjs for this project.

tecoad avatar Jan 26 '22 22:01 tecoad

Might be the same problem in my situation: I try to access the dev server via a reverse proxy. I have tried kit.paths.base and kit.vite.server.origin. Nothing works to set the sub-path associated on the proxy machine. Still showing import { start } from "/@fs/ which should use some subdirectory...

Beiri22 avatar Feb 10 '22 14:02 Beiri22

My team uses nginx + docker-compose to set up a dev environment with multiple APIs/frontends. Since we only have one svelte-kit project, this is ok for now, but it is not ideal. In order to get the current version of svelte-kit to work in this set up, we need to set up nginx with the below configuration. I'd expect that analogous configuration is required in whatever reverse proxy environment is being used. (HMR config is separate, but easier to implement because it's on a different port).

        location /svelte-kit-ui-base-path {
            set $target "http://svelte_kit_ui:3000";
            proxy_pass $target;
        }
        location  /@fs {
            set $target "http://svelte_kit_ui:3000";
            proxy_pass $target;
        }
        location /.svelte-kit {
            set $target "http://svelte_kit_ui:3000";
            proxy_pass $target;
        }
        location /node_modules {
            set $target "http://svelte_kit_ui:3000";
            proxy_pass $target;
        }
        location /@vite/client {
            set $target "http://svelte_kit_ui:3000";
            proxy_pass $target;
        }
        location /src {
            set $target "http://svelte_kit_ui:3000";
            proxy_pass $target;
        }

I'd view this as a work-around, not a long-term solution. Hopefully this saves time for anyone else having trouble with this.

It would be very nice to be able to prefix every route above with /svelte-kit-ui-base-path.

davidroeca avatar Feb 14 '22 16:02 davidroeca

I am running a svelte-kit dev server behind a code-server proxy and can confirm that this is still an issue. Any fix or workaround available?

mosherbrian avatar Mar 01 '22 03:03 mosherbrian

I am still getting this issue. Any fixes?

Edit bug seemed to have fixed itself (possibly by updating my sveltekit)

AlbertMarashi avatar Apr 03 '22 10:04 AlbertMarashi

I am still getting this issue. Any fixes?

squelix avatar Apr 27 '22 12:04 squelix

Pretty sure this is fixed now — are people still experiencing it with latest versions? If so, a repro would be much appreciated

Rich-Harris avatar May 17 '22 22:05 Rich-Harris

I'll try out on my example project as soon as possible.

Beiri22 avatar May 18 '22 06:05 Beiri22

@Rich-Harris The normal case works. But we have a special case here. The application is working with Shibboleth authentication that is carried out by an reverse proxy server. But all the "/@fs/..."-Links are not put under the base path. So I cannot use the dev mode. Is there any way to approach this behaviour?

By the way - in reverse proxy mode it would be nice to have an option that the base path is only applied when creating the pages. What do I mean: The page has a link to "/base/path/some.page", the reverse proxy removes the base path, so the server serves the document without the base path "/some.page"

Beiri22 avatar May 18 '22 07:05 Beiri22

I just tried it out on my project with svelte-kit, 1.0.0-next.335

I'm still having the problem. I think @Beiri22 is correct, the /@fs/... paths don't have the base path so it's unable to load those supporting files in dev mode.

andycarlberg avatar May 18 '22 16:05 andycarlberg

How would I go about reproducing this?

Rich-Harris avatar May 19 '22 16:05 Rich-Harris

  1. Create a new SvelteKit project
  2. Set kit.paths.base to a non-root path
  3. Run the dev server

If you're just running the dev server locally, you will be able to navigate the project. Check the browser dev tools and note the paths that are being requested. The /@fs/... paths don't include the base path prefix. This is the actual issue that needs to be addressed.

It works for local development because the local dev server has access to both the base path and the non-prefixed paths. It becomes a problem for development behind a proxy where the base path is necessary to access any files on the dev server. In my case, this is surfaced when I'm trying to use code-server to develop. The dev server isn't accessible locally because it isn't running locally. Instead, I access it through a proxied base path. It works for the initial page load but those /@fs/... paths can't be reached without the base path prefix.

andycarlberg avatar May 19 '22 17:05 andycarlberg