1- # Next.js Runtime Environment Configuration
2-
31![ GitHub branch checks state] [ build-url ] [ ![ codecov] [ cov-img ]] [ cov-url ]
42
5- Populates your environment at ** runtime** rather than ** build time** .
3+ # Next.js Runtime Environment Configuration
4+
5+ Populate your environment at ** runtime** rather than ** build time** .
66
77- Isomorphic - Server and browser compatible (and even in middleware.)
88- Static site generation support.
@@ -49,7 +49,7 @@ containing allow-listed environment variables with a `NEXT_PUBLIC_` prefix.
4949
50502 . Add the following to the head section of your ` pages/_document.js ` :
5151
52- ``` jsx
52+ ``` tsx
5353// pages/_document.tsx
5454<script src = " /__ENV.js" />
5555```
@@ -59,21 +59,20 @@ This will load the generated file in the browser.
5959### Usage 🧑💻
6060
6161In the browser, your variables will now be available at
62- ` window.__ENV.NEXT_PUBLIC_FOO ` and on the server at ` process.env.NEXT_PUBLIC_FOO ` .
63-
64- #### Helper function 🙌
65-
66- We have included the ` env() ` helper function to make retrieving a value easier:
62+ ` window.__ENV.NEXT_PUBLIC_FOO ` and on the server at
63+ ` process.env.NEXT_PUBLIC_FOO ` . For example:
6764
6865``` bash
6966# .env
7067NEXT_PUBLIC_FOO=" foo"
7168BAR=" bar"
69+ NEXT_PUBLIC_BAZ=" baz"
7270```
7371
74- ``` jsx
72+ ``` tsx
73+ // pages/some-page.tsx
7574type Props = {
76- bar: string,
75+ bar: string ;
7776};
7877
7978export default function SomePage({ bar }: Props ) {
@@ -93,13 +92,24 @@ export const getStaticProps: GetStaticProps = async (context) => {
9392};
9493```
9594
96- Becomes...
95+ ### Utilities 🛠
9796
98- ``` jsx
97+ We have included some utility function to make it even easier to work with
98+ environment variables.
99+
100+ #### ` env(key: string): string | undefined `
101+
102+ Returns the value of the environment variable with the given key. If the
103+ environment variable is not found, it returns undefined.
104+
105+ ##### Example
106+
107+ ``` tsx
108+ // pages/some-page.tsx
99109import { env } from ' next-runtime-env' ;
100110
101111type Props = {
102- bar: string,
112+ bar: string ;
103113};
104114
105115export default function SomePage({ bar }: Props ) {
@@ -119,6 +129,70 @@ export const getStaticProps: GetStaticProps = async (context) => {
119129};
120130```
121131
132+ #### ` allEnv(): NodeJS.ProcessEnv `
133+
134+ Returns all environment variables as a ` NodeJS.ProcessEnv ` object regardless of
135+ the platform. This is useful if you want to destructure multiple env vars at
136+ once.
137+
138+ ##### Example
139+
140+ ``` tsx
141+ // pages/some-page.tsx
142+ import { allEnv } from ' next-runtime-env' ;
143+
144+ type Props = {
145+ bar: string ;
146+ };
147+
148+ export default function SomePage({ bar }: Props ) {
149+ const { NEXT_PUBLIC_FOO, NEXT_PUBLIC_BAZ } = allEnv ();
150+
151+ return (
152+ <div >
153+ { NEXT_PUBLIC_FOO } { NEXT_PUBLIC_BAZ } { bar }
154+ </div >
155+ );
156+ }
157+
158+ export const getStaticProps: GetStaticProps = async (context ) => {
159+ const { BAR } = allEnv ();
160+
161+ return {
162+ props: {
163+ bar: BAR ,
164+ },
165+ };
166+ };
167+ ```
168+
169+ #### ` makeEnvPublic(key: string | string[]): void `
170+
171+ Makes an environment variable with a given key public. This is useful if you
172+ want to use an environment variable in the browser, but it was was not declared
173+ with a ` NEXT_PUBLIC_ ` prefix.
174+
175+ For ease of use you can also make multiple env vars public at once by passing an
176+ array of keys.
177+
178+ ##### Example
179+
180+ ``` js
181+ // next.config.js
182+ const { configureRuntimeEnv } = require (' next-runtime-env/build/configure' );
183+ const { makeEnvPublic } = require (' next-runtime-env/build/make-env-public' );
184+
185+ // Given that `FOO` is declared as a regular env var, not a public one. This
186+ // will make it public and available as `NEXT_PUBLIC_FOO`.
187+ makeEnvPublic (' FOO' );
188+
189+ // Or you can make multiple env vars public at once.
190+ makeEnvPublic ([' BAR' , ' BAZ' ]);
191+
192+ // This will generate the `__ENV.js` file and include `NEXT_PUBLIC_FOO`.
193+ configureRuntimeEnv ();
194+ ```
195+
122196### Maintenance 👷
123197
124198This package is maintained and actively used by [ Expatfile.tax] [ expatfile-site ] .
0 commit comments