base configuration option can break vite's raw fs handler in devserver #7520
Labels
- P3: minor bug
An edge case that only affects very specific usage (priority)
requires refactor
Bug, may take longer as fixing either requires refactors, breaking changes, or considering tradeoffs
What version of
astro
are you using?2.7.1
Are you using an SSR adapter? If so, which one?
None
What package manager are you using?
npm
What operating system are you using?
Linux
What browser are you using?
Chrome
Describe the Bug
If you set the base config option to a part of a folder name that exists in the root, vite will not be able to load any files under that path.
This code right here:
https://github.com/withastro/astro/blob/main/packages/astro/src/vite-plugin-astro-server/plugin.ts#L46-L52
Places the 'base' middleware before the raw fs handler, and in the base middleware there's this bit of code:
https://github.com/withastro/astro/blob/main/packages/astro/src/vite-plugin-astro-server/base.ts#L25-L28
If you create an astro.config with the base option set to say, 'src' (like the linked example) where all your files are located, this will cause the urls to any files loaded on the client to get converted from
/src/my-file.ts
to/my-file.ts
, which will result in a 404 as the file is not at that path.This also happens if you set trailingSlashes to never, in that case it breaks any file with the same prefix as your base path (so in my case, i had a base path of '/no', which turns all '/node_modules' to '/de_modules')
My workaround for now is to move the mw around in the devserver with a vite plugin, so that the base middleware is placed just before astroDevHandler, and moving the viteServePublicMiddleware to run after the base middleware as those files should have their base path stripped.
I'm not sure what the cleanest real solution would be, there are some quick fixes like not modifying the URL if there's an "astro" querystring parameter, but '@vite/client' etc scripts does not include that, so there could still be issues.
What's the expected result?
The raw fs handler in vite should be able to serve any local files in the project regardless of the base configuration option value.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-hajh6e
Participation
The text was updated successfully, but these errors were encountered: