Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show error if vite client cannot be loaded #17419

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/vite/src/node/server/middlewares/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ export function errorMiddleware(
<meta charset="UTF-8" />
<title>Error</title>
<script type="module">
import { ErrorOverlay } from '/@vite/client'
document.body.appendChild(new ErrorOverlay(${JSON.stringify(
prepareError(err),
).replace(/</g, '\\u003c')}))
try {
const { ErrorOverlay } = await import('/@vite/client')
document.body.appendChild(new ErrorOverlay(${JSON.stringify(
prepareError(err),
).replace(/</g, '\\u003c')}))
} catch {
document.innerHTML = \`<h1>Internal Server Error</h1><h2>Error overlay failed to load</h2><pre>${err.stack?.replaceAll('<', '&lt;').replaceAll('$', '&dollar;')}</pre>\`
}
Comment on lines +80 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Could we have the JSON.stringify(prepareError(err)).replace(...) kept in a variable outside the try-block, and re-use it here too? That way we have a consistent formatting of the error (e.g. stacktrace is ansi-stripped), and we don't need to escape here differently.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd originally tried doing the same .replace(/</g, '\\u003c'), but it wasn't working. I don't fully understand why it needs different escaping here or the difference between them. In any case, we would still need an extra escaping of $ in this second use because it's being used as a template string, which is different than the first use

</script>
</head>
<body>
Expand Down