1
- # Next.js Runtime Environment Configuration
2
-
3
1
![ GitHub branch checks state] [ build-url ] [ ![ codecov] [ cov-img ]] [ cov-url ]
4
2
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** .
6
6
7
7
- Isomorphic - Server and browser compatible (and even in middleware.)
8
8
- Static site generation support.
@@ -49,7 +49,7 @@ containing allow-listed environment variables with a `NEXT_PUBLIC_` prefix.
49
49
50
50
2 . Add the following to the head section of your ` pages/_document.js ` :
51
51
52
- ``` jsx
52
+ ``` tsx
53
53
// pages/_document.tsx
54
54
<script src = " /__ENV.js" />
55
55
```
@@ -59,21 +59,20 @@ This will load the generated file in the browser.
59
59
### Usage 🧑💻
60
60
61
61
In 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:
67
64
68
65
``` bash
69
66
# .env
70
67
NEXT_PUBLIC_FOO=" foo"
71
68
BAR=" bar"
69
+ NEXT_PUBLIC_BAZ=" baz"
72
70
```
73
71
74
- ``` jsx
72
+ ``` tsx
73
+ // pages/some-page.tsx
75
74
type Props = {
76
- bar: string,
75
+ bar: string ;
77
76
};
78
77
79
78
export default function SomePage({ bar }: Props ) {
@@ -93,13 +92,24 @@ export const getStaticProps: GetStaticProps = async (context) => {
93
92
};
94
93
```
95
94
96
- Becomes...
95
+ ### Utilities 🛠
97
96
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
99
109
import { env } from ' next-runtime-env' ;
100
110
101
111
type Props = {
102
- bar: string,
112
+ bar: string ;
103
113
};
104
114
105
115
export default function SomePage({ bar }: Props ) {
@@ -119,6 +129,70 @@ export const getStaticProps: GetStaticProps = async (context) => {
119
129
};
120
130
```
121
131
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
+
122
196
### Maintenance 👷
123
197
124
198
This package is maintained and actively used by [ Expatfile.tax] [ expatfile-site ] .
0 commit comments