Skip to content

Commit a917b04

Browse files
committed
feat: ✨ clean up next script opt-out
1 parent b8423a4 commit a917b04

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

src/script/env-script.spec.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('EnvScript', () => {
5454
<EnvScript
5555
env={env}
5656
nonce={nonce}
57-
nextScriptComponentProps={{
57+
nextScriptProps={{
5858
strategy: 'afterInteractive',
5959
id,
6060
}}
@@ -75,8 +75,8 @@ describe('EnvScript', () => {
7575
render(
7676
<EnvScript
7777
env={env}
78-
withNextScriptComponent={false}
79-
nextScriptComponentProps={{
78+
disableNextScript
79+
nextScriptProps={{
8080
id,
8181
}}
8282
/>,

src/script/env-script.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// XXX: Blocked by https://github.com/vercel/next.js/pull/58129
22
// import { headers } from 'next/headers';
3-
import Script, { ScriptProps } from 'next/script';
3+
import Script, { type ScriptProps } from 'next/script';
44
import { type FC } from 'react';
55

66
import { type NonceConfig } from '../typings/nonce';
@@ -10,8 +10,8 @@ import { PUBLIC_ENV_KEY } from './constants';
1010
type EnvScriptProps = {
1111
env: ProcessEnv;
1212
nonce?: string | NonceConfig;
13-
withNextScriptComponent?: boolean;
14-
nextScriptComponentProps?: ScriptProps;
13+
disableNextScript?: boolean;
14+
nextScriptProps?: ScriptProps;
1515
};
1616

1717
/**
@@ -28,8 +28,8 @@ type EnvScriptProps = {
2828
export const EnvScript: FC<EnvScriptProps> = ({
2929
env,
3030
nonce,
31-
withNextScriptComponent = true,
32-
nextScriptComponentProps = { strategy: 'beforeInteractive' },
31+
disableNextScript = false,
32+
nextScriptProps = { strategy: 'beforeInteractive' },
3333
}) => {
3434
let nonceString: string | undefined;
3535

@@ -43,23 +43,23 @@ export const EnvScript: FC<EnvScriptProps> = ({
4343
nonceString = nonce;
4444
}
4545

46-
const html = {
46+
const innerHTML = {
4747
__html: `window['${PUBLIC_ENV_KEY}'] = ${JSON.stringify(env)}`,
4848
};
4949

5050
// You can opt to use a regular "<script>" tag instead of Next.js' Script Component.
5151
// Note: When using Sentry, sentry.client.config.ts might run after the Next.js <Script> component, even when the strategy is "beforeInteractive"
5252
// 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} />;
53+
if (disableNextScript) {
54+
return <script nonce={nonceString} dangerouslySetInnerHTML={innerHTML} />;
5555
}
5656

5757
// Use Next.js Script Component by default
5858
return (
5959
<Script
60-
{...nextScriptComponentProps}
60+
{...nextScriptProps}
6161
nonce={nonceString}
62-
dangerouslySetInnerHTML={html}
62+
dangerouslySetInnerHTML={innerHTML}
6363
/>
6464
);
6565
};

src/script/public-env-script.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { unstable_noStore as noStore } from 'next/cache';
22
import { type FC } from 'react';
3+
import { type ScriptProps } from 'next/script';
34

45
import { getPublicEnv } from '../helpers/get-public-env';
56
import { type NonceConfig } from '../typings/nonce';
67
import { EnvScript } from './env-script';
78

89
type PublicEnvScriptProps = {
910
nonce?: string | NonceConfig;
11+
disableNextScript?: boolean;
12+
nextScriptProps?: ScriptProps;
1013
};
1114

1215
/**
@@ -23,11 +26,22 @@ type PublicEnvScriptProps = {
2326
* </head>
2427
* ```
2528
*/
26-
export const PublicEnvScript: FC<PublicEnvScriptProps> = ({ nonce }) => {
29+
export const PublicEnvScript: FC<PublicEnvScriptProps> = ({
30+
nonce,
31+
disableNextScript,
32+
nextScriptProps,
33+
}) => {
2734
noStore(); // Opt into dynamic rendering
2835

2936
// This value will be evaluated at runtime
3037
const publicEnv = getPublicEnv();
3138

32-
return <EnvScript env={publicEnv} nonce={nonce} />;
39+
return (
40+
<EnvScript
41+
env={publicEnv}
42+
nonce={nonce}
43+
disableNextScript={disableNextScript}
44+
nextScriptProps={nextScriptProps}
45+
/>
46+
);
3347
};

0 commit comments

Comments
 (0)