From d501e5b21b897bb6223cb4ece74c1f050b469fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benny=20Powers=20-=20=D7=A2=D7=9D=20=D7=99=D7=A9=D7=A8?= =?UTF-8?q?=D7=90=D7=9C=20=D7=97=D7=99!?= Date: Tue, 27 Aug 2024 12:35:50 +0300 Subject: [PATCH] fix(core): improved ssr (#2833) --- core/pfe-core/package.json | 5 ++++ core/pfe-core/ssr-shims.ts | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 core/pfe-core/ssr-shims.ts diff --git a/core/pfe-core/package.json b/core/pfe-core/package.json index d44afee046..6a23731fd1 100644 --- a/core/pfe-core/package.json +++ b/core/pfe-core/package.json @@ -11,6 +11,11 @@ "exports": { ".": "./core.js", "./*": "./*", + "./ssr-shims.js": { + "node": "./ssr-shims.js", + "import": "./core.js", + "default": "./core.js" + }, "./controllers/*": "./controllers/*", "./decorators/*": "./decorators/*", "./functions/*": "./functions/*", diff --git a/core/pfe-core/ssr-shims.ts b/core/pfe-core/ssr-shims.ts new file mode 100644 index 0000000000..30a6c904c9 --- /dev/null +++ b/core/pfe-core/ssr-shims.ts @@ -0,0 +1,56 @@ +class ObserverShim { + observe(): void { + void 0; + } + + disconnect(): void { + void 0; + } +} + +class MiniHTMLElement { + innerHTML = ''; + constructor(public tagName: string) { } +} + +class MiniHTMLTemplateElement extends MiniHTMLElement { + content = { cloneNode: (): string => this.innerHTML }; +} + +class MiniDocument { + createElement(tagName: string): MiniHTMLElement { + switch (tagName) { + case 'template': + return new MiniHTMLTemplateElement(tagName); + default: + return new MiniHTMLElement(tagName); + } + } +} + +// @ts-expect-error: this runs in node +globalThis.window ??= globalThis; +// @ts-expect-error: this runs in node +globalThis.document ??= new MiniDocument(); +// @ts-expect-error: this runs in node +globalThis.navigator ??= { userAgent: '' }; +// @ts-expect-error: this runs in node +globalThis.ErrorEvent ??= Event; +// @ts-expect-error: this runs in node +globalThis.IntersectionObserver ??= ObserverShim; +// @ts-expect-error: this runs in node +globalThis.MutationObserver ??= ObserverShim; +// @ts-expect-error: this runs in node +globalThis.getComputedStyle ??= function() { + return { + getPropertyPriority() { + return ''; + }, + getPropertyValue() { + return ''; + }, + }; +} + +; +