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

Conversation

benmccann
Copy link
Collaborator

@benmccann benmccann commented Jun 7, 2024

Description

fixes #17418

Ensure that we show an error if /@vite/client cannot be loaded. Prior to this PR the error handler worked only with post middlewares. Now it works with all middlewares

Copy link

stackblitz bot commented Jun 7, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@benmccann benmccann force-pushed the benmccann-patch-1 branch 4 times, most recently from 9cb1c1c to 609d437 Compare June 8, 2024 05:23
@benmccann benmccann modified the milestone: 5.3 Jun 8, 2024
@benmccann benmccann added the p3-minor-bug An edge case that only affects very specific usage (priority) label Jun 8, 2024
@bluwy
Copy link
Member

bluwy commented Jun 10, 2024

I feel we could improve the DX by having the error overlay support a "standalone" mode or "non-dismissable" mode? That way the error overlay is non-dismissable (as there's no reason so), and if they accidentally clicked outside, they won't get a lousier UI.

I also think we should only render the fallback error if the error overlay fail to load? We should also add an additional message that the error overlay failed to load.

@benmccann
Copy link
Collaborator Author

Those sound like reasonable ideas, but are all more complicated to implement and are just UX niceties. Maybe we could move ahead with this to fix the core issue of having a blank page and then file an issue for the followup ideas

@bluwy
Copy link
Member

bluwy commented Jun 10, 2024

I don't think the issue is urgent enough that we should go ahead with this middleground, since the error can be viewed from the terminal too? So I'm leaning on doing a proper fix instead 😬

@benmccann
Copy link
Collaborator Author

I also think we should only render the fallback error if the error overlay fail to load? We should also add an additional message that the error overlay failed to load.

I made both of these changes

I feel we could improve the DX by having the error overlay support a "standalone" mode or "non-dismissable" mode? That way the error overlay is non-dismissable (as there's no reason so), and if they accidentally clicked outside, they won't get a lousier UI.

This part really feels separate to me. The current PR is purely an improvement over the current state. In the cases where there previously was a blank page we're now showing an error and in the cases where the overlay was rendering it's exactly the same. I do like the idea of a non-dismissable mode, but think we could handle that in a separate PR

@bluwy
Copy link
Member

bluwy commented Jun 13, 2024

Thanks! Yeah fair we can handle that separately 👍

Comment on lines +80 to +87
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>\`
}
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error middleware cannot display errors from plugin middlewares
2 participants