Describe the bug
transformIndexHtml() prints the following error when HTML contains a relative link to a script, and url 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:
|
} else if (url[0] === '.' || isBareRelative(url)) { |
|
preTransformUrl = path.posix.join( |
|
config.base, |
|
path.posix.dirname(htmlPath), |
|
url, |
|
) |
|
} |
When using builtin vite server, it converts urls that end in slash to absolute filesystem paths that end in index.html:
|
// trailing slash should check for fallback index.html |
|
else if (pathname[pathname.length - 1] === '/') { |
|
const filePath = path.join(root, pathname, 'index.html') |
|
if (fs.existsSync(filePath)) { |
|
const newUrl = url + 'index.html' |
|
debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`) |
|
req.url = newUrl |
|
return next() |
|
} |
|
} |
but this doesn't happen for the SSR example since:
- It uses
appType: 'custom', which disables this url preprocessing
- The custom handler uses
req.originalUrl, which is not modified
- The HTML file may not exist
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-bx1fvf91?file=index.js
Steps to reproduce
npm install
npm run dev
- Observe the error message in console:
Pre-transform error: Failed to load url /example/filename.js (resolved id: /example/filename.js). Does the file exist?
- Ctrl+C
- Change
/example/dir/ on line 4 to example/dir/index.html
npm run dev
- Observe no error
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
- Don't use
originalUrl (it breaks if base is set to some directory). Use req.url`
- Remove query parameters an hashes from the url, so that only the path is left
- If it ends in
/, add index.html to it
- Pass this path to
transformIndexHtml
Describe the bug
transformIndexHtml()prints the following error when HTML contains a relative link to a script, andurlparameter 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 installnpm run devPre-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.htmlnpm run devSystem 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.11Used Package Manager
npm
Logs
No response
Validations
Workaround
originalUrl(it breaks ifbaseis set to some directory). Usereq.url`/, addindex.htmlto ittransformIndexHtml