Skip to content

Commit

Permalink
fix(core): improved ssr (#2833)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers authored Aug 27, 2024
1 parent 8681f15 commit d501e5b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/pfe-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*",
Expand Down
56 changes: 56 additions & 0 deletions core/pfe-core/ssr-shims.ts
Original file line number Diff line number Diff line change
@@ -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 '';
},
};
}

;

0 comments on commit d501e5b

Please sign in to comment.