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: check the existence of "document" before using it #15262

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 13 additions & 4 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,27 @@ function notifyListeners(event: string, data: any): void {
}

const enableOverlay = __HMR_ENABLE_OVERLAY__
const hasDocument = typeof document !== 'undefined'

function createErrorOverlay(err: ErrorPayload['err']) {
clearErrorOverlay()
document.body.appendChild(new ErrorOverlay(err))
if (hasDocument) {
document.body.appendChild(new ErrorOverlay(err))
}
}

function clearErrorOverlay() {
document.querySelectorAll<ErrorOverlay>(overlayId).forEach((n) => n.close())
if (hasDocument) {
document.querySelectorAll<ErrorOverlay>(overlayId).forEach((n) => n.close())
}
}

function hasErrorOverlay() {
return document.querySelectorAll(overlayId).length
if (hasDocument) {
return document.querySelectorAll(overlayId).length
}

return false
}

let pending = false
Expand Down Expand Up @@ -378,7 +387,7 @@ const sheetsMap = new Map<string, HTMLStyleElement>()

// collect existing style elements that may have been inserted during SSR
// to avoid FOUC or duplicate styles
if ('document' in globalThis) {
if (hasDocument) {
document
.querySelectorAll<HTMLStyleElement>('style[data-vite-dev-id]')
.forEach((el) => {
Expand Down