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(html): Show error overlay when parsing invalid file #6184

Merged
merged 5 commits into from Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -9,3 +9,4 @@ LICENSE.md
pnpm-lock.yaml
pnpm-workspace.yaml
packages/playground/tsconfig-json-load-error/has-error/tsconfig.json
packages/playground/html/invalid.html
28 changes: 27 additions & 1 deletion packages/playground/html/__tests__/html.spec.ts
@@ -1,4 +1,4 @@
import { getColor, isBuild } from '../../testUtils'
import { getColor, isBuild, editFile } from '../../testUtils'

function testPage(isNested: boolean) {
test('pre transform', async () => {
Expand Down Expand Up @@ -210,3 +210,29 @@ describe('unicode path', () => {
expect(await page.textContent('h1')).toBe('unicode-path')
})
})

if (!isBuild) {
describe('invalid', () => {
test('should be 500 with overlay', async () => {
const response = await page.goto(viteTestUrl + '/invalid.html')
expect(response.status()).toBe(500)

const errorOverlay = await page.waitForSelector('vite-error-overlay')
expect(errorOverlay).toBeTruthy()

const message = await errorOverlay.$$eval('.message-body', (m) => {
return m[0].innerHTML
})
expect(message).toMatch(/^Unable to parse HTML/)
})

test('should reload when fixed', async () => {
const response = await page.goto(viteTestUrl + '/invalid.html')
await editFile('invalid.html', (content) => {
return content.replace('<div Bad', '<div> Good')
})
const content = await page.waitForSelector('text=Good Html')
expect(content).toBeTruthy()
})
})
}
1 change: 1 addition & 0 deletions packages/playground/html/invalid.html
@@ -0,0 +1 @@
<div Bad Html</div>
2 changes: 2 additions & 0 deletions packages/vite/src/client/client.ts
Expand Up @@ -485,3 +485,5 @@ export function injectQuery(url: string, queryToInject: string): string {
hash || ''
}`
}

export { ErrorOverlay }
18 changes: 17 additions & 1 deletion packages/vite/src/node/server/middlewares/error.ts
Expand Up @@ -69,7 +69,23 @@ export function errorMiddleware(
next()
} else {
res.statusCode = 500
res.end()
res.end(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Error</title>
<script type="module">
import { ErrorOverlay } from '/@vite/client'
document.body.appendChild(new ErrorOverlay(${JSON.stringify(
prepareError(err)
).replace(/</g, '\\u003c')}))
</script>
</head>
<body>
</body>
</html>
`)
}
}
}