Skip to content

Commit 98a4fcc

Browse files
Fix SSR build
1 parent caee01b commit 98a4fcc

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/shared/lib/csp/addNonceToStyles.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const originalAppendChild = Node.prototype.appendChild;
2-
const originalInsertBefore = Node.prototype.insertBefore;
1+
const { SSR, MODE } = import.meta.env;
2+
3+
const globalNode = !SSR ? Node : ({ prototype: {} } as typeof Node);
4+
const originalAppendChild = globalNode.prototype.appendChild;
5+
const originalInsertBefore = globalNode.prototype.insertBefore;
36

47
const assetsFolder =
5-
import.meta.env.MODE === "production"
6-
? "/assets/"
7-
: "/node_modules/.vite/deps/";
8+
MODE === "production" ? "/assets/" : "/node_modules/.vite/deps/";
89

910
/**
1011
* Check if the current chunk is one of those that do not support nonce
@@ -39,6 +40,9 @@ function isSelectedChunk(chunksWithoutNonce: string[]) {
3940
* Add nonce to style elements that do not have it yet for the selected chunks
4041
*/
4142
export function addNonceToStyles(nonce: string, chunksWithoutNonce: string[]) {
43+
if (SSR) {
44+
return;
45+
}
4246
const addNonceToStyle = (element: Node) => {
4347
const isStyleElement = element instanceof HTMLStyleElement;
4448
if (!isStyleElement) {
@@ -57,12 +61,12 @@ export function addNonceToStyles(nonce: string, chunksWithoutNonce: string[]) {
5761
element.setAttribute("nonce", nonce);
5862
};
5963

60-
Node.prototype.appendChild = function <T extends Node>(node: T): T {
64+
globalNode.prototype.appendChild = function <T extends Node>(node: T): T {
6165
addNonceToStyle(node);
6266
return originalAppendChild.call(this, node) as T;
6367
};
6468

65-
Node.prototype.insertBefore = function <T extends Node>(
69+
globalNode.prototype.insertBefore = function <T extends Node>(
6670
node: T,
6771
referenceNode: Node | null,
6872
): T {

0 commit comments

Comments
 (0)