kit: adapter-node doesn't work correctly with paths.base
Describe the bug
Basically, we have this svelte.config.js
.
import adapter from '@sveltejs/adapter-node';
const config = {
kit: {
adapter: adapter(),
paths: {
base: '/basepath'
},
}
};
export default config;
When we now run svelte-kit build
, it produces this folder structure (partly):
build
client
_app
...
server
...
static
...
the problem now is that the browser is not able to fetch the files in client
and static
. The node server returns status code 404
for these files.
The browser requests the files like this: http://localhost:3000/basepath/_app/...
.
If we manually modify the folder structure to this (manually add a basepath
folder in client
and static
) it works fine.
build
client
basepath
_app
...
server
...
static
basepath
...
It seems that the adapter-node
forgets to take paths.base
into account for the static files.
By the way: If I start the build with svelte-kit preview
, it works fine. Just starting it with node build
does not work.
Reproduction
I created a simple demo app based on the npm init svelte@next
command.
https://github.com/vekunz/svelte-paths-base-demo
Logs
basepath:1 GET http://localhost:3000/basepath 404 (Not Found)
basepath:9 GET http://localhost:3000/basepath/_app/assets/pages/__layout.svelte-ace81755.css net::ERR_ABORTED 404 (Not Found)
basepath:10 GET http://localhost:3000/basepath/_app/start-5dffe62e.js net::ERR_ABORTED 404 (Not Found)
basepath:19 GET http://localhost:3000/basepath/_app/assets/svelte-logo-87df40b8.svg 404 (Not Found)
basepath:13 GET http://localhost:3000/basepath/_app/error.svelte-bf7b1a86.js net::ERR_ABORTED 404 (Not Found)
basepath:11 GET http://localhost:3000/basepath/_app/chunks/vendor-ee294e9e.js net::ERR_ABORTED 404 (Not Found)
basepath:12 GET http://localhost:3000/basepath/_app/pages/__layout.svelte-1bb88ece.js net::ERR_ABORTED 404 (Not Found)
System Info
System:
OS: Windows 10 10.0.18363
CPU: (12) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
Memory: 16.61 GB / 31.79 GB
Binaries:
Node: 14.18.1 - C:\Program Files\nodejs\node.EXE
npm: 6.14.15 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 97.0.4692.99
Edge: Spartan (44.18362.1593.0)
Internet Explorer: 11.0.18362.1766
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.17
@sveltejs/adapter-node: ^1.0.0-next.67 => 1.0.0-next.67
@sveltejs/kit: next => 1.0.0-next.260
svelte: ^3.46.0 => 3.46.4
Severity
blocking all usage of SvelteKit
Additional Information
It seems that in my example app moving the static files in a basepath
subdirectory does not work also.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 31
- Comments: 23 (3 by maintainers)
Commits related to this issue
- Allow builder to access the bath config variable * Add `base` to builder * Prepend `builder.base` when copying Client, Static and Prerendered assets in adapter-node Fixes #3726 Related #3620 — committed to bundabrg/kit by bundabrg 2 years ago
- [feat] use config.kit.paths.base for static assets (#4448) fixes #4442, fixes #2843, fixes #3726 Write static files to a folder that takes into account the base path value — committed to sveltejs/kit by hmnd 2 years ago
Same issue. Software is not usable for now.
I personally think that this is actually part of a bigger issue and simply copying to the correct folder whilst a quick fix is not a “good fix”. I’ve added changes to my branch that does the quick fix but I don’t like it.
When generating a static site we ultimately will be placing it either in the correct folder OR preferably behind a load-balancing reverse proxy that strips off the prefix and have it served from the root. So the prefix should not exist in the build folder at all but the code will need to know to add the prefix when referring to assets.
When generating a dynamic site it can be passed environment variables (or perhaps read a config file) where it gets its base prefix. In the same way the site could exist behind a reverse proxy that strips off the prefix OR it’ll handle the prefix itself. It matters not as this is an implementation detail of the adapter. The code needs to know to add the prefix when referring to assets and presumably when doing SSR fetching it’ll need to know to add/remove the prefix as necessary.
adapter-node
usesPolka
to serve its assets, executed with the following code:handler
is provided as:The issue is that
ssr
will strip offbase
from the beginning of the URL but the threeserve
calls do not. Thessr
function IMHO should not be doing this and instead everything should be mounted at thepolka().use()
call, something like this:where
BASE_PATH
would be set either by reading the config file or come in from an environment variable.This way if I have a reverse proxy that strips prefixes I can have
BASE_PATH
set to ‘’ but pass the actual base through a header likeX-Forwarded-Prefix
for when building URL’s.If I don’t strip the prefix, then I can set
BASE_PATH
to the prefix so its handled inside Polka instead but otherwise the rest of the code remains the same.Anyway, that’s my 2c.
Any updates here? I am currently evaluating using Svelte(Kit) in our new app and this would be a showstopper.
The bug the majority in this issue have is fixed now, the base path is taken into account when writing the files to disk. This doesn’t suffice for people with reserved proxies which strip the base path or something related. #7242 tracks that.
This works, but you still need to specifically set
export const prerender = false;
fornpm run build
to work with out errors.otherwise you stil get this
with sveltekig.config.js set like this
Makes me wonder. Why do most build tools assume apps will run on the root? “Hello World” apps maybe, but not “Real World” apps.
base should ALWAYS be a variable, defaulting to “/” maybe yes, but never assume it’s not there.
Two comments:
npm run build
withpaths.base
set to, say"/myapp"
(sveltekit’s documentation says it should not end with a slash), vite complains it should instead end with a slash? Something is wrong there (see line(!) "base" option should end with a slash.
:This bug still exists in the current version. The workaround of moving both the client/_app folder and the contents of the client/static folder to the right place works though.
Similar issues with adapter-static - see #3620 Incorrect build path for prerendered endpoints when setting a basepath
I get the same warning: “(!) “base” option should end with a slash.”
I think I figured out where the change needs to happen. In
adapter-node/index.js
the line that reads:should change to
I suspect the
writePrerendered
method call might need modifying too, but I don’t have an example app using prerendering to test that on.I also suspect their is a similar change in
adapter-static
that will fix the same issue.Oh, I really need this to be fixed. I cannot use the workaround mentioned earlier because my case is one level more complex as I need to use the generated middleware handler in another express server (directus). I cannot control exactly when to
.use(handler)
and they bind many other middlewares which means that I have to bind the sveltekit handler only to a single subpath (say/dashboard
)..use(handler)
without settingpaths.base
Both frontends (sveltekit and directus) are confused and nothing works…
.use(handler)
with settingpaths.base
to/dashboard
Sveltekit frontend displays html pages but scripts are not found. But directus does not load correctly.
.use('/dashboard', handler)
without settingpaths.base
I can access the sveltekit frontend via
/dashboard
but internal links (href=“{base}/about”) are not working asbase
is empty and javascript cannot be loaded (urls do not contain/dashboard
and thus are not handled by the middleware). Directus works without probems..use('/dashboard', handler)
with settingpaths.base
to/dashboard
I can access kit via
/dashboard/dashboard
(yes duplicated) but the the about page is not available at/dashboard/about
(which is build from href=“{base}/about”) but at/dashboard/dashboard/about
. Basically I need to duplicate the base path. Strangely now the javascript files can be loaded correctly (at /dashboard/_app/immutable/start-e25cee58.js).I found I can fix this by removing these lines https://github.com/sveltejs/kit/blob/2a9c19252912d61bd840441fe80dc13366af41f7/packages/kit/src/runtime/server/index.js#L58-L63 Now everything works as expected (i.e.
/dashboard
and/dashboard/about
) at least with my minimal testing.This is surely not the final solution as the dev server needs those lines and I guess the other adapters need them as well…
I got one step further by adding the base url to the request before passing it to the
handler
:Now the
ssr
function works correctly (because it strips the basepath we just added) but the static files are not correctly served (because sirv does not strip the basepath) as @bundabrg also noted in https://github.com/sveltejs/kit/issues/3726#issuecomment-1049460340This is what I am doing to automate the directory structure updates. I chain this script onto my build command. This works for me and I am using this approach for all my Svelte Kit apps (for now).
@seanlail, I think I may still be importing base in my components too. I’ll have to check if that is a necessary step or not. My script doesn’t move server, and I’m pretty sure the server directory has nothing to do with this issue.
As already mentioned, there is a related issue with the static adapter - https://github.com/sveltejs/kit/issues/4528. The workaround of adjusting the directories didn’t work for me but I found a workaround with the following.