diff --git a/src/js/utils/server-safe-globals.js b/src/js/utils/server-safe-globals.js index 810aa11af..02ea1ac82 100644 --- a/src/js/utils/server-safe-globals.js +++ b/src/js/utils/server-safe-globals.js @@ -10,8 +10,17 @@ class ResizeObserver { observe() {} } +const documentShim = { + createElement: function () { + return new globalThisShim.HTMLElement(); + }, + addEventListener() {}, + removeEventListener() {}, +}; + const globalThisShim = { ResizeObserver, + document: documentShim, HTMLElement: class HTMLElement extends EventTarget {}, DocumentFragment: class DocumentFragment extends EventTarget {}, customElements: { @@ -27,18 +36,13 @@ const globalThisShim = { } }; -const documentShim = { - createElement: function () { - return new globalThisShim.HTMLElement(); - }, - addEventListener() {}, - removeEventListener() {}, -}; - export const isServer = typeof window === 'undefined' || typeof window.customElements === 'undefined'; +const isShimmed = Object.keys(globalThisShim) + .every(key => key in globalThis); + /** * @type { globalThis & { * WebKitPlaybackTargetAvailabilityEvent?, @@ -66,7 +70,7 @@ export const isServer = * CastableVideoElement? * } } * */ -export const GlobalThis = isServer ? globalThisShim : globalThis; +export const GlobalThis = isServer && !isShimmed ? globalThisShim : globalThis; /** * @type { document & { webkitExitFullscreen? } | @@ -82,7 +86,7 @@ export const GlobalThis = isServer ? globalThisShim : globalThis; * removeEventListener?, * } } */ -export const Document = isServer ? documentShim : window.document; +export const Document = isServer && !isShimmed ? documentShim : globalThis.document; export { GlobalThis as globalThis,