Skip to content

Commit

Permalink
fix(overlay): detect document, respect overlay option (#1958)
Browse files Browse the repository at this point in the history
Co-authored-by: neverland <[email protected]>
  • Loading branch information
fi3ework and chenjiahan authored Mar 29, 2024
1 parent b00ca9d commit 66a6ebb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/itchy-lions-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rsbuild/core': patch
---

fix(overlay): detect document, respect overlay option
4 changes: 3 additions & 1 deletion packages/core/src/client/hmr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ function onMessage(e: MessageEvent<string>) {
const message = JSON.parse(e.data);
switch (message.type) {
case 'hash':
clearOverlay();
if (enableOverlay) {
clearOverlay();
}
handleAvailableHash(message.data);
break;
case 'still-ok':
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/client/hmr/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,25 @@ if (customElements && !customElements.get(overlayId)) {
customElements.define(overlayId, ErrorOverlay);
}

const documentAvailable = typeof document !== 'undefined';

export function createOverlay(err: string[]) {
if (!documentAvailable) {
console.info(
'Failed to display Rsbuild overlay since document is not available, considering turning off the `dev.client.overlay` option.',
);
return;
}

clearOverlay();
document.body.appendChild(new ErrorOverlay(err));
}

export function clearOverlay() {
if (!documentAvailable) {
return;
}

// use NodeList's forEach api instead of dom.iterable
// biome-ignore lint/complexity/noForEach: <explanation>
document.querySelectorAll<ErrorOverlay>(overlayId).forEach((n) => n.close());
Expand Down

0 comments on commit 66a6ebb

Please sign in to comment.