11// XXX: Blocked by https://github.com/vercel/next.js/pull/58129
22// import { headers } from 'next/headers';
3- import Script from 'next/script' ;
43import { type FC } from 'react' ;
54
65import { type NonceConfig } from '../typings/nonce' ;
76import { type ProcessEnv } from '../typings/process-env' ;
87import { PUBLIC_ENV_KEY } from './constants' ;
8+ import Script , { ScriptProps } from 'next/script' ;
99
1010type EnvScriptProps = {
1111 env : ProcessEnv ;
1212 nonce ?: string | NonceConfig ;
13+ withNextScriptComponent ?: boolean ;
14+ nextScriptComponentProps ?: ScriptProps ;
1315} ;
1416
1517/**
@@ -23,7 +25,12 @@ type EnvScriptProps = {
2325 * </head>
2426 * ```
2527 */
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+ } ) => {
2734 let nonceString : string | undefined ;
2835
2936 // XXX: Blocked by https://github.com/vercel/next.js/pull/58129
@@ -36,13 +43,23 @@ export const EnvScript: FC<EnvScriptProps> = ({ env, nonce }) => {
3643 nonceString = nonce ;
3744 }
3845
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
3958 return (
4059 < Script
41- strategy = "beforeInteractive"
60+ { ... nextScriptComponentProps }
4261 nonce = { nonceString }
43- dangerouslySetInnerHTML = { {
44- __html : `window['${ PUBLIC_ENV_KEY } '] = ${ JSON . stringify ( env ) } ` ,
45- } }
62+ dangerouslySetInnerHTML = { html }
4663 />
4764 ) ;
4865} ;
0 commit comments