UNPKG

@ryankshaw/next-runtime-env

Version:

Next.js Runtime Environment Configuration - Populates your environment at runtime rather than build time.

26 lines 1.11 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { connection } from 'next/server.js'; import { PUBLIC_ENV_KEY } from './constants.js'; import { startsWithNextPublic, } from '../helpers/next-public-utils.js'; /** * Sets the public environment variables in the browser. Forwards any other props (like `nonce`) on to the <script> tag. * * This component is disables Next.js' caching mechanism to ensure that the * environment variables are always up-to-date. * * Usage: * ```ts * <head> * <PublicEnvScript /> * </head> * ``` */ export async function PublicEnvScript({ whitelist, ...otherProps }) { await connection(); // Makes sure this is dynamically rendered at runtime // This will be evaluated at runtime const publicEnv = Object.fromEntries(Object.entries(process.env).filter(([key]) => startsWithNextPublic(key) && (!whitelist || whitelist.includes(key)))); return (_jsx("script", { ...otherProps, dangerouslySetInnerHTML: { __html: `window['${PUBLIC_ENV_KEY}'] = ${JSON.stringify(publicEnv)}`, } })); } //# sourceMappingURL=public-env-script.js.map