Skip to content
Merged
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
39 changes: 23 additions & 16 deletions src/runtime/html-validate/nitro.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,31 @@ export default <NitroAppPlugin> function (nitro) {

nitro.hooks.hook('render:response', async (response, { event }) => {
if (typeof response.body === 'string' && (response.headers?.['Content-Type'] || response.headers?.['content-type'])?.includes('html')) {
const formattedBody = await format(response.body, { plugins: [html], parser: 'html' })
const results = await validator.validateString(formattedBody)
try {
const formattedBody = await format(response.body, { plugins: [html], parser: 'html' })
const results = await validator.validateString(formattedBody)
Comment on lines +47 to +48

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.


if (response.body && results.errorCount > 0) {
const id = randomUUID()
const data: HtmlValidateReport = {
id,
path: event.path,
html: formattedBody,
results: results.results,
if (response.body && results.errorCount > 0) {
const id = randomUUID()
const data: HtmlValidateReport = {
id,
path: event.path,
html: formattedBody,
results: results.results,
}
response.body = addBeforeBodyEndTag(
response.body,
`<script id="hints-html-validate" type="application/json">${stringify(data)}</script>`,
)
nitro.hooks.callHook(HTML_VALIDATE_REPORT_HOOK, data).catch((error) => {
nitro.captureError(error instanceof Error ? error : new Error(String(error)), { event })
})
}
response.body = addBeforeBodyEndTag(
response.body,
`<script id="hints-html-validate" type="application/json">${stringify(data)}</script>`,
)
nitro.hooks.callHook(HTML_VALIDATE_REPORT_HOOK, data).catch((error) => {
nitro.captureError(error instanceof Error ? error : new Error(String(error)), { event })
})
}
catch (error) {
// https://github.com/nuxt/hints/issues/360
// html validate can throw errors for some html
console.warn('HTML Validate error:', error instanceof Error ? error.message : String(error))
}
}
})
Expand Down
Loading