SSR dev server "Pre-transform error: Failed to load url" for relative path #18964
Labels
contribution welcome
feat: html
p3-minor-bug
An edge case that only affects very specific usage (priority)
Describe the bug
transformIndexHtml()
prints the following error when HTML contains a relative link to a script, andurl
parameter ends in slash (i.e./example/dir/
, and not/example/dir/index.html
):Pre-transform error: Failed to load url /example/filename.js (resolved id: /example/filename.js). Does the file exist?
This can occur when implementing the dev server following the SSR guide.
Cause
This happens because
dirname()
of the html url is taken which, for a path that ends in slash, removes the directory name. And the file is searched in the parent directory:vite/packages/vite/src/node/server/middlewares/indexHtml.ts
Lines 160 to 166 in 63b82f1
When using builtin vite server, it converts urls that end in slash to absolute filesystem paths that end in index.html:
vite/packages/vite/src/node/server/middlewares/htmlFallback.ts
Lines 44 to 53 in 63b82f1
but this doesn't happen for the SSR example since:
appType: 'custom'
, which disables this url preprocessingreq.originalUrl
, which is not modifiedReproduction
https://stackblitz.com/edit/vitest-dev-vitest-bx1fvf91?file=index.js
Steps to reproduce
npm install
npm run dev
Pre-transform error: Failed to load url /example/filename.js (resolved id: /example/filename.js). Does the file exist?
/example/dir/
on line 4 toexample/dir/index.html
npm run dev
System Info
System: OS: Linux 5.0 undefined CPU: (2) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 18.20.3 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 10.2.3 - /usr/local/bin/npm pnpm: 8.15.6 - /usr/local/bin/pnpm npmPackages: vite: latest => 5.4.11
Used Package Manager
npm
Logs
No response
Validations
Workaround
originalUrl
(it breaks ifbase
is set to some directory). Use
req.url`/
, addindex.html
to ittransformIndexHtml
The text was updated successfully, but these errors were encountered: