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 ;
3
6
4
7
const assetsFolder =
5
- import . meta. env . MODE === "production"
6
- ? "/assets/"
7
- : "/node_modules/.vite/deps/" ;
8
+ MODE === "production" ? "/assets/" : "/node_modules/.vite/deps/" ;
8
9
9
10
/**
10
11
* Check if the current chunk is one of those that do not support nonce
@@ -39,6 +40,9 @@ function isSelectedChunk(chunksWithoutNonce: string[]) {
39
40
* Add nonce to style elements that do not have it yet for the selected chunks
40
41
*/
41
42
export function addNonceToStyles ( nonce : string , chunksWithoutNonce : string [ ] ) {
43
+ if ( SSR ) {
44
+ return ;
45
+ }
42
46
const addNonceToStyle = ( element : Node ) => {
43
47
const isStyleElement = element instanceof HTMLStyleElement ;
44
48
if ( ! isStyleElement ) {
@@ -57,12 +61,12 @@ export function addNonceToStyles(nonce: string, chunksWithoutNonce: string[]) {
57
61
element . setAttribute ( "nonce" , nonce ) ;
58
62
} ;
59
63
60
- Node . prototype . appendChild = function < T extends Node > ( node : T ) : T {
64
+ globalNode . prototype . appendChild = function < T extends Node > ( node : T ) : T {
61
65
addNonceToStyle ( node ) ;
62
66
return originalAppendChild . call ( this , node ) as T ;
63
67
} ;
64
68
65
- Node . prototype . insertBefore = function < T extends Node > (
69
+ globalNode . prototype . insertBefore = function < T extends Node > (
66
70
node : T ,
67
71
referenceNode : Node | null ,
68
72
) : T {
0 commit comments