Skip to content

Commit 3feb6c0

Browse files
davevanhoornHofmannZ
authored andcommitted
feat: ✨ add options to use Next Script Component or a regular script tag
1 parent 669f9cb commit 3feb6c0

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ jobs:
8585
JEST_JUNIT_CLASSNAME: '{filepath}'
8686

8787
- name: Upload code coverage
88-
uses: codecov/codecov-action@v4
88+
uses: codecov/codecov-action@v5
8989
with:
9090
token: ${{ secrets.CODECOV_TOKEN }}

src/script/env-script.spec.tsx

+40
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,46 @@ describe('EnvScript', () => {
4545
expect(document.querySelector('script')).not.toHaveAttribute('nonce');
4646
});
4747

48+
it('should accept Next.js Script tag props', () => {
49+
const env = { NODE_ENV: 'test', API_URL: 'http://localhost:3000' };
50+
const nonce = 'test-nonce-xyz';
51+
const id = 'text-id-abc';
52+
53+
render(
54+
<EnvScript
55+
env={env}
56+
nonce={nonce}
57+
nextScriptComponentProps={{
58+
strategy: 'afterInteractive',
59+
id,
60+
}}
61+
/>,
62+
);
63+
64+
expect(document.querySelector('script')).toHaveAttribute('nonce', nonce);
65+
expect(document.querySelector('script')).toHaveAttribute('id', id);
66+
expect(document.querySelector('script')?.textContent).toBe(
67+
`window['__ENV'] = ${JSON.stringify(env)}`,
68+
);
69+
});
70+
71+
it('should not have Next.js Script props when a regular script tag is used', () => {
72+
const env = { NODE_ENV: 'test', API_URL: 'http://localhost:3000' };
73+
const id = 'text-id-abc';
74+
75+
render(
76+
<EnvScript
77+
env={env}
78+
withNextScriptComponent={false}
79+
nextScriptComponentProps={{
80+
id,
81+
}}
82+
/>,
83+
);
84+
85+
expect(document.querySelector('script')).not.toHaveAttribute('id', id);
86+
});
87+
4888
it.todo(
4989
'should get the nonce from the headers when the headerKey is provided',
5090
);

src/script/env-script.tsx

+23-6
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 from 'next/script';
3+
import Script, { ScriptProps } from 'next/script';
44
import { type FC } from 'react';
55

66
import { type NonceConfig } from '../typings/nonce';
@@ -10,6 +10,8 @@ import { PUBLIC_ENV_KEY } from './constants';
1010
type 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

Comments
 (0)