Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for strategy Prop from Next.js Script Library #150

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/script/env-script.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// XXX: Blocked by https://github.com/vercel/next.js/pull/58129
// import { headers } from 'next/headers';
import Script from 'next/script';
import Script, { ScriptProps } from 'next/script';
import { type FC } from 'react';

import { type NonceConfig } from '../typings/nonce';
Expand All @@ -10,6 +10,7 @@ import { PUBLIC_ENV_KEY } from './constants';
type EnvScriptProps = {
env: ProcessEnv;
nonce?: string | NonceConfig;
strategy?: ScriptProps['strategy'];
};

/**
Expand All @@ -23,7 +24,7 @@ type EnvScriptProps = {
* </head>
* ```
*/
export const EnvScript: FC<EnvScriptProps> = ({ env, nonce }) => {
export const EnvScript: FC<EnvScriptProps> = ({ env, nonce, strategy }) => {
let nonceString: string | undefined;

// XXX: Blocked by https://github.com/vercel/next.js/pull/58129
Expand All @@ -38,7 +39,7 @@ export const EnvScript: FC<EnvScriptProps> = ({ env, nonce }) => {

return (
<Script
strategy="beforeInteractive"
strategy={strategy || 'beforeInteractive'}
nonce={nonceString}
dangerouslySetInnerHTML={{
__html: `window['${PUBLIC_ENV_KEY}'] = ${JSON.stringify(env)}`,
Expand Down
9 changes: 7 additions & 2 deletions src/script/public-env-script.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { unstable_noStore as noStore } from 'next/cache';
import { ScriptProps } from 'next/script';
import { type FC } from 'react';

import { getPublicEnv } from '../helpers/get-public-env';
Expand All @@ -7,6 +8,7 @@ import { EnvScript } from './env-script';

type PublicEnvScriptProps = {
nonce?: string | NonceConfig;
strategy?: ScriptProps['strategy'];
};

/**
Expand All @@ -23,11 +25,14 @@ type PublicEnvScriptProps = {
* </head>
* ```
*/
export const PublicEnvScript: FC<PublicEnvScriptProps> = ({ nonce }) => {
export const PublicEnvScript: FC<PublicEnvScriptProps> = ({
nonce,
strategy,
}) => {
noStore(); // Opt into dynamic rendering

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

return <EnvScript env={publicEnv} nonce={nonce} />;
return <EnvScript env={publicEnv} nonce={nonce} strategy={strategy} />;
};