Skip to content

feat(preview-server): Proper error handling for prettier's invalid HTML error #2265

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

Open
wants to merge 19 commits into
base: canary
Choose a base branch
from

Conversation

gabrielmfern
Copy link
Collaborator

@gabrielmfern gabrielmfern commented May 26, 2025

This PR introduces a custom JSX runtime that we use for rendering all emails that allow for tying HTML elements back into the React source file and line. It does that so that it can add the line into the start of the stack trace of the React code that actually ends up rendering as invalid HTML pointed out by prettier:

t6 {
  span: h {
    start: t2 { file: [De], offset: 8029, line: 0, col: 8029 },
    end: t2 { file: [De], offset: 8033, line: 0, col: 8033 },
    fullStart: t2 { file: [De], offset: 8029, line: 0, col: 8029 },
    details: null
  },
  msg: 'Unexpected closing tag "p". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that
-have-implied-end-tags',
  level: 1,
  elementName: 'p'
}

The way we get the attribute from the prettier error, though, can fail because we try to find the closest attribute to the offset given by prettier in the html. So, this means it might actually point to the next node instead of the correct one, but only the next one. Not sure if we can circumvent this without having to actually parse the HTML ourselves through some sort of lenient parser.

The JSX runtime has only the dev variant and is just a very thin wrapper around the one from react/jsx-dev-runtime that we bundle in with esbuild to get the user's version of it and not the preview server's. This change can also benefit other places like the different checkers

It also improves the error overlay's UI quite a lot, along with customizing how the parsing error message gets rendered. So, the end result is

image

Copy link

changeset-bot bot commented May 26, 2025

🦋 Changeset detected

Latest commit: ca8032d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@react-email/preview-server Patch
react-email Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented May 26, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-email ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2025 2:59pm
react-email-demo ❌ Failed (Inspect) May 28, 2025 2:59pm

Copy link

pkg-pr-new bot commented May 26, 2025

Open in StackBlitz

npm i https://pkg.pr.new/resend/react-email/@react-email/preview-server@2265
npm i https://pkg.pr.new/resend/react-email@2265

commit: ca8032d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant