1
1
// XXX: Blocked by https://github.com/vercel/next.js/pull/58129
2
2
// import { headers } from 'next/headers';
3
- import Script from 'next/script' ;
3
+ import Script , { ScriptProps } from 'next/script' ;
4
4
import { type FC } from 'react' ;
5
5
6
6
import { type NonceConfig } from '../typings/nonce' ;
@@ -10,6 +10,8 @@ import { PUBLIC_ENV_KEY } from './constants';
10
10
type EnvScriptProps = {
11
11
env : ProcessEnv ;
12
12
nonce ?: string | NonceConfig ;
13
+ withNextScriptComponent ?: boolean ;
14
+ nextScriptComponentProps ?: ScriptProps ;
13
15
} ;
14
16
15
17
/**
@@ -23,7 +25,12 @@ type EnvScriptProps = {
23
25
* </head>
24
26
* ```
25
27
*/
26
- export const EnvScript : FC < EnvScriptProps > = ( { env, nonce } ) => {
28
+ export const EnvScript : FC < EnvScriptProps > = ( {
29
+ env,
30
+ nonce,
31
+ withNextScriptComponent = true ,
32
+ nextScriptComponentProps = { strategy : 'beforeInteractive' } ,
33
+ } ) => {
27
34
let nonceString : string | undefined ;
28
35
29
36
// XXX: Blocked by https://github.com/vercel/next.js/pull/58129
@@ -36,13 +43,23 @@ export const EnvScript: FC<EnvScriptProps> = ({ env, nonce }) => {
36
43
nonceString = nonce ;
37
44
}
38
45
46
+ const html = {
47
+ __html : `window['${ PUBLIC_ENV_KEY } '] = ${ JSON . stringify ( env ) } ` ,
48
+ } ;
49
+
50
+ // You can opt to use a regular "<script>" tag instead of Next.js' Script Component.
51
+ // Note: When using Sentry, sentry.client.config.ts might run after the Next.js <Script> component, even when the strategy is "beforeInteractive"
52
+ // This results in the runtime environments being undefined and the Sentry client config initialized without the correct configuration.
53
+ if ( ! withNextScriptComponent ) {
54
+ return < script nonce = { nonceString } dangerouslySetInnerHTML = { html } /> ;
55
+ }
56
+
57
+ // Use Next.js Script Component by default
39
58
return (
40
59
< Script
41
- strategy = "beforeInteractive"
60
+ { ... nextScriptComponentProps }
42
61
nonce = { nonceString }
43
- dangerouslySetInnerHTML = { {
44
- __html : `window['${ PUBLIC_ENV_KEY } '] = ${ JSON . stringify ( env ) } ` ,
45
- } }
62
+ dangerouslySetInnerHTML = { html }
46
63
/>
47
64
) ;
48
65
} ;
0 commit comments