Skip to content

Commit ef1f627

Browse files
committed
GH-43: Fix "ReferenceError: document is not defined" issue for SSR
1 parent dfa9e86 commit ef1f627

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/styles.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ export const jsonToCss = (stylesheet: object) => {
1111

1212
export const injectStyles = (cssText: string) => {
1313
/** Inject the given `cssText` in the document head */
14-
const style = document.createElement("style");
15-
style.setAttribute("type", "text/css");
14+
try {
15+
const style = document.createElement("style");
16+
style.setAttribute("type", "text/css");
1617

17-
if ((style as any).styleSheet) {
18-
(style as any).styleSheet.cssText = cssText;
19-
} else {
20-
style.appendChild(document.createTextNode(cssText));
21-
}
18+
if ((style as any).styleSheet) {
19+
(style as any).styleSheet.cssText = cssText;
20+
} else {
21+
style.appendChild(document.createTextNode(cssText));
22+
}
2223

23-
document.head.appendChild(style);
24+
document.head.appendChild(style);
25+
} catch (err) {
26+
}
2427
}

0 commit comments

Comments
 (0)