Skip to content

Commit 150846a

Browse files
committed
fix: check document before showing errors/reloading
1 parent 1960075 commit 150846a

File tree

2 files changed

+77
-72
lines changed

2 files changed

+77
-72
lines changed

packages/vite/src/client/client.ts

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ function setupWebSocket(
102102

103103
notifyListeners('vite:ws:disconnect', { webSocket: socket })
104104

105-
console.log(`[vite] server connection lost. polling for restart...`)
106-
await waitForSuccessfulPing(protocol, hostAndPath)
107-
location.reload()
105+
if (hasDocument) {
106+
console.log(`[vite] server connection lost. polling for restart...`)
107+
await waitForSuccessfulPing(protocol, hostAndPath)
108+
location.reload()
109+
}
108110
})
109111

110112
return socket
@@ -182,18 +184,20 @@ async function handleMessage(payload: HMRPayload) {
182184
break
183185
case 'update':
184186
notifyListeners('vite:beforeUpdate', payload)
185-
// if this is the first update and there's already an error overlay, it
186-
// means the page opened with existing server compile error and the whole
187-
// module script failed to load (since one of the nested imports is 500).
188-
// in this case a normal update won't work and a full reload is needed.
189-
if (isFirstUpdate && hasErrorOverlay()) {
190-
window.location.reload()
191-
return
192-
} else {
193-
if (enableOverlay) {
194-
clearErrorOverlay()
187+
if (hasDocument) {
188+
// if this is the first update and there's already an error overlay, it
189+
// means the page opened with existing server compile error and the whole
190+
// module script failed to load (since one of the nested imports is 500).
191+
// in this case a normal update won't work and a full reload is needed.
192+
if (isFirstUpdate && hasErrorOverlay()) {
193+
window.location.reload()
194+
return
195+
} else {
196+
if (enableOverlay) {
197+
clearErrorOverlay()
198+
}
199+
isFirstUpdate = false
195200
}
196-
isFirstUpdate = false
197201
}
198202
await Promise.all(
199203
payload.updates.map(async (update): Promise<void> => {
@@ -251,21 +255,23 @@ async function handleMessage(payload: HMRPayload) {
251255
}
252256
case 'full-reload':
253257
notifyListeners('vite:beforeFullReload', payload)
254-
if (payload.path && payload.path.endsWith('.html')) {
255-
// if html file is edited, only reload the page if the browser is
256-
// currently on that page.
257-
const pagePath = decodeURI(location.pathname)
258-
const payloadPath = base + payload.path.slice(1)
259-
if (
260-
pagePath === payloadPath ||
261-
payload.path === '/index.html' ||
262-
(pagePath.endsWith('/') && pagePath + 'index.html' === payloadPath)
263-
) {
258+
if (hasDocument) {
259+
if (payload.path && payload.path.endsWith('.html')) {
260+
// if html file is edited, only reload the page if the browser is
261+
// currently on that page.
262+
const pagePath = decodeURI(location.pathname)
263+
const payloadPath = base + payload.path.slice(1)
264+
if (
265+
pagePath === payloadPath ||
266+
payload.path === '/index.html' ||
267+
(pagePath.endsWith('/') && pagePath + 'index.html' === payloadPath)
268+
) {
269+
pageReload()
270+
}
271+
return
272+
} else {
264273
pageReload()
265274
}
266-
return
267-
} else {
268-
pageReload()
269275
}
270276
break
271277
case 'prune':
@@ -274,13 +280,15 @@ async function handleMessage(payload: HMRPayload) {
274280
break
275281
case 'error': {
276282
notifyListeners('vite:error', payload)
277-
const err = payload.err
278-
if (enableOverlay) {
279-
createErrorOverlay(err)
280-
} else {
281-
console.error(
282-
`[vite] Internal Server Error\n${err.message}\n${err.stack}`,
283-
)
283+
if (hasDocument) {
284+
const err = payload.err
285+
if (enableOverlay) {
286+
createErrorOverlay(err)
287+
} else {
288+
console.error(
289+
`[vite] Internal Server Error\n${err.message}\n${err.stack}`,
290+
)
291+
}
284292
}
285293
break
286294
}
@@ -300,6 +308,7 @@ function notifyListeners(event: string, data: any): void {
300308
}
301309

302310
const enableOverlay = __HMR_ENABLE_OVERLAY__
311+
const hasDocument = 'document' in globalThis
303312

304313
function createErrorOverlay(err: ErrorPayload['err']) {
305314
clearErrorOverlay()

packages/vite/src/client/overlay.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -165,42 +165,40 @@ kbd {
165165
`
166166

167167
// Error Template
168-
const template =
169-
'document' in globalThis
170-
? h(
168+
const createTemplate = () =>
169+
h(
170+
'div',
171+
{ class: 'backdrop', part: 'backdrop' },
172+
h(
173+
'div',
174+
{ class: 'window', part: 'window' },
175+
h(
176+
'pre',
177+
{ class: 'message', part: 'message' },
178+
h('span', { class: 'plugin', part: 'plugin' }),
179+
h('span', { class: 'message-body', part: 'message-body' }),
180+
),
181+
h('pre', { class: 'file', part: 'file' }),
182+
h('pre', { class: 'frame', part: 'frame' }),
183+
h('pre', { class: 'stack', part: 'stack' }),
184+
h(
171185
'div',
172-
{ class: 'backdrop', part: 'backdrop' },
173-
h(
174-
'div',
175-
{ class: 'window', part: 'window' },
176-
h(
177-
'pre',
178-
{ class: 'message', part: 'message' },
179-
h('span', { class: 'plugin', part: 'plugin' }),
180-
h('span', { class: 'message-body', part: 'message-body' }),
181-
),
182-
h('pre', { class: 'file', part: 'file' }),
183-
h('pre', { class: 'frame', part: 'frame' }),
184-
h('pre', { class: 'stack', part: 'stack' }),
185-
h(
186-
'div',
187-
{ class: 'tip', part: 'tip' },
188-
'Click outside, press ',
189-
h('kbd', {}, 'Esc'),
190-
' key, or fix the code to dismiss.',
191-
h('br'),
192-
'You can also disable this overlay by setting ',
193-
h('code', { part: 'config-option-name' }, 'server.hmr.overlay'),
194-
' to ',
195-
h('code', { part: 'config-option-value' }, 'false'),
196-
' in ',
197-
h('code', { part: 'config-file-name' }, hmrConfigName),
198-
'.',
199-
),
200-
),
201-
h('style', {}, templateStyle),
202-
)
203-
: undefined
186+
{ class: 'tip', part: 'tip' },
187+
'Click outside, press ',
188+
h('kbd', {}, 'Esc'),
189+
' key, or fix the code to dismiss.',
190+
h('br'),
191+
'You can also disable this overlay by setting ',
192+
h('code', { part: 'config-option-name' }, 'server.hmr.overlay'),
193+
' to ',
194+
h('code', { part: 'config-option-value' }, 'false'),
195+
' in ',
196+
h('code', { part: 'config-file-name' }, hmrConfigName),
197+
'.',
198+
),
199+
),
200+
h('style', {}, templateStyle),
201+
)
204202

205203
const fileRE = /(?:[a-zA-Z]:\\|\/).*?:\d+:\d+/g
206204
const codeframeRE = /^(?:>?\s*\d+\s+\|.*|\s+\|\s*\^.*)\r?\n/gm
@@ -216,9 +214,7 @@ export class ErrorOverlay extends HTMLElement {
216214
super()
217215
this.root = this.attachShadow({ mode: 'open' })
218216

219-
if (template) {
220-
this.root.appendChild(template)
221-
}
217+
this.root.appendChild(createTemplate())
222218

223219
codeframeRE.lastIndex = 0
224220
const hasFrame = err.frame && codeframeRE.test(err.frame)

0 commit comments

Comments
 (0)