diff --git a/src/cdk/overlay/overlay.ts b/src/cdk/overlay/overlay.ts index 6584da581763..c82136f254fe 100644 --- a/src/cdk/overlay/overlay.ts +++ b/src/cdk/overlay/overlay.ts @@ -68,7 +68,10 @@ export function createOverlayRef(injector: Injector, config?: OverlayConfig): Ov overlayConfig.direction = overlayConfig.direction || directionality.value; - if (!('showPopover' in doc.body)) { + // `document.body` can be null during page navigation or unload cycles per the WHATWG spec + // (https://html.spec.whatwg.org/multipage/dom.html#dom-document-body), even though TypeScript + // types it as non-nullable. Guard against it to avoid "Cannot use 'in' operator ... in null". + if (!doc.body || !('showPopover' in doc.body)) { overlayConfig.usePopover = false; } else { overlayConfig.usePopover = config?.usePopover ?? defaultUsePopover;