Skip to content

fix(html-validate): wrap validation in a try/catch - #377

Merged
huang-julien merged 2 commits into
mainfrom
fix/html-validate
Aug 1, 2026
Merged

fix(html-validate): wrap validation in a try/catch#377
huang-julien merged 2 commits into
mainfrom
fix/html-validate

Conversation

@huang-julien

Copy link
Copy Markdown
Member

🔗 Linked issue

fix #360

📚 Description

wraps the validation in a try/catch statement. html-validate can throw with unexpected tags

@pkg-pr-new

pkg-pr-new Bot commented Aug 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nuxt/hints@377

commit: 382463d

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ca541123-469e-4117-b213-9af7b53fd546

📥 Commits

Reviewing files that changed from the base of the PR and between 39f8df2 and 382463d.

📒 Files selected for processing (1)
  • src/runtime/html-validate/nitro.plugin.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/runtime/html-validate/nitro.plugin.ts

📝 Walkthrough

Walkthrough

The Nitro render:response hook now wraps response formatting, HTML validation, report injection, and report-hook notification in a try/catch. Formatting or validation errors are logged as warnings with the error message or string representation. Successful validation retains the existing behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The change catches formatter errors but does not show a fallback to response.body or report injection when formatting fails. Fallback to response.body when formatting fails, then run html-validate and inject the inline report so affected SSR responses return HTTP 200.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: wrapping html-validate processing in a try/catch.
Description check ✅ Passed The description explains that html-validate errors are wrapped to prevent SSR failures, which matches the changeset.
Out of Scope Changes check ✅ Passed The changes remain focused on preventing html-validate processing errors from failing SSR responses.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/html-validate

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/runtime/html-validate/nitro.plugin.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/runtime/html-validate/nitro.plugin.ts`:
- Around line 47-48: Separate Prettier formatting from validation in the handler
around formattedBody and validator.validateString: if format fails, fall back to
the original response.body, validate that string, and assign the same fallback
string to HtmlValidateReport.html. Preserve the existing reporting and
hook-event flow, and add a regression test covering the <foreignObject><picture>
case.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ffbf050e-4139-40de-a37e-ab381230bc2e

📥 Commits

Reviewing files that changed from the base of the PR and between 6e57ef2 and 39f8df2.

📒 Files selected for processing (1)
  • src/runtime/html-validate/nitro.plugin.ts

Comment on lines +47 to +48
const formattedBody = await format(response.body, { plugins: [html], parser: 'html' })
const results = await validator.validateString(formattedBody)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Continue validation when formatting fails.

format and validator.validateString share one try block. If Prettier throws at Line [47], validation never processes the original HTML. The response avoids the HTTP 500, but it also loses the inline hints-html-validate report and the hints:html-validate:report hook event.

Catch the formatting error separately, validate response.body as the fallback, and set HtmlValidateReport.html to the same string. Add a regression test for the <foreignObject><picture> case.

Proposed fix
       try {
-        const formattedBody = await format(response.body, { plugins: [html], parser: 'html' })
-        const results = await validator.validateString(formattedBody)
+        let htmlToValidate = response.body
+        try {
+          htmlToValidate = await format(response.body, { plugins: [html], parser: 'html' })
+        }
+        catch (error) {
+          console.warn('HTML formatting error:', error instanceof Error ? error.message : String(error))
+        }
+        const results = await validator.validateString(htmlToValidate)
...
-            html: formattedBody,
+            html: htmlToValidate,

Also applies to: 66-69

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/runtime/html-validate/nitro.plugin.ts` around lines 47 - 48, Separate
Prettier formatting from validation in the handler around formattedBody and
validator.validateString: if format fails, fall back to the original
response.body, validate that string, and assign the same fallback string to
HtmlValidateReport.html. Preserve the existing reporting and hook-event flow,
and add a regression test covering the <foreignObject><picture> case.

@huang-julien
huang-julien merged commit df0dd5d into main Aug 1, 2026
9 checks passed
@huang-julien
huang-julien deleted the fix/html-validate branch August 1, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

htmlValidate Nitro plugin crashes SSR responses (HTTP 500) on SVG <foreignObject> containing a <picture>

1 participant