Skip to content

Commit 1f945f6

Browse files
authored
fix(html): show error overlay when parsing invalid file (vitejs#6184)
1 parent 1d722c5 commit 1f945f6

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ LICENSE.md
99
pnpm-lock.yaml
1010
pnpm-workspace.yaml
1111
packages/playground/tsconfig-json-load-error/has-error/tsconfig.json
12+
packages/playground/html/invalid.html

packages/playground/html/__tests__/html.spec.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getColor, isBuild } from '../../testUtils'
1+
import { getColor, isBuild, editFile } from '../../testUtils'
22

33
function testPage(isNested: boolean) {
44
test('pre transform', async () => {
@@ -210,3 +210,29 @@ describe('unicode path', () => {
210210
expect(await page.textContent('h1')).toBe('unicode-path')
211211
})
212212
})
213+
214+
if (!isBuild) {
215+
describe('invalid', () => {
216+
test('should be 500 with overlay', async () => {
217+
const response = await page.goto(viteTestUrl + '/invalid.html')
218+
expect(response.status()).toBe(500)
219+
220+
const errorOverlay = await page.waitForSelector('vite-error-overlay')
221+
expect(errorOverlay).toBeTruthy()
222+
223+
const message = await errorOverlay.$$eval('.message-body', (m) => {
224+
return m[0].innerHTML
225+
})
226+
expect(message).toMatch(/^Unable to parse HTML/)
227+
})
228+
229+
test('should reload when fixed', async () => {
230+
const response = await page.goto(viteTestUrl + '/invalid.html')
231+
await editFile('invalid.html', (content) => {
232+
return content.replace('<div Bad', '<div> Good')
233+
})
234+
const content = await page.waitForSelector('text=Good Html')
235+
expect(content).toBeTruthy()
236+
})
237+
})
238+
}

packages/playground/html/invalid.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div Bad Html</div>

packages/vite/src/client/client.ts

+2
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,5 @@ export function injectQuery(url: string, queryToInject: string): string {
485485
hash || ''
486486
}`
487487
}
488+
489+
export { ErrorOverlay }

packages/vite/src/node/server/middlewares/error.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,23 @@ export function errorMiddleware(
6969
next()
7070
} else {
7171
res.statusCode = 500
72-
res.end()
72+
res.end(`
73+
<!DOCTYPE html>
74+
<html lang="en">
75+
<head>
76+
<meta charset="UTF-8" />
77+
<title>Error</title>
78+
<script type="module">
79+
import { ErrorOverlay } from '/@vite/client'
80+
document.body.appendChild(new ErrorOverlay(${JSON.stringify(
81+
prepareError(err)
82+
).replace(/</g, '\\u003c')}))
83+
</script>
84+
</head>
85+
<body>
86+
</body>
87+
</html>
88+
`)
7389
}
7490
}
7591
}

0 commit comments

Comments
 (0)