Skip to content

Commit d501e5b

Browse files
authored
fix(core): improved ssr (#2833)
1 parent 8681f15 commit d501e5b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

core/pfe-core/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"exports": {
1212
".": "./core.js",
1313
"./*": "./*",
14+
"./ssr-shims.js": {
15+
"node": "./ssr-shims.js",
16+
"import": "./core.js",
17+
"default": "./core.js"
18+
},
1419
"./controllers/*": "./controllers/*",
1520
"./decorators/*": "./decorators/*",
1621
"./functions/*": "./functions/*",

core/pfe-core/ssr-shims.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class ObserverShim {
2+
observe(): void {
3+
void 0;
4+
}
5+
6+
disconnect(): void {
7+
void 0;
8+
}
9+
}
10+
11+
class MiniHTMLElement {
12+
innerHTML = '';
13+
constructor(public tagName: string) { }
14+
}
15+
16+
class MiniHTMLTemplateElement extends MiniHTMLElement {
17+
content = { cloneNode: (): string => this.innerHTML };
18+
}
19+
20+
class MiniDocument {
21+
createElement(tagName: string): MiniHTMLElement {
22+
switch (tagName) {
23+
case 'template':
24+
return new MiniHTMLTemplateElement(tagName);
25+
default:
26+
return new MiniHTMLElement(tagName);
27+
}
28+
}
29+
}
30+
31+
// @ts-expect-error: this runs in node
32+
globalThis.window ??= globalThis;
33+
// @ts-expect-error: this runs in node
34+
globalThis.document ??= new MiniDocument();
35+
// @ts-expect-error: this runs in node
36+
globalThis.navigator ??= { userAgent: '' };
37+
// @ts-expect-error: this runs in node
38+
globalThis.ErrorEvent ??= Event;
39+
// @ts-expect-error: this runs in node
40+
globalThis.IntersectionObserver ??= ObserverShim;
41+
// @ts-expect-error: this runs in node
42+
globalThis.MutationObserver ??= ObserverShim;
43+
// @ts-expect-error: this runs in node
44+
globalThis.getComputedStyle ??= function() {
45+
return {
46+
getPropertyPriority() {
47+
return '';
48+
},
49+
getPropertyValue() {
50+
return '';
51+
},
52+
};
53+
}
54+
55+
;
56+

0 commit comments

Comments
 (0)