Skip to content

Commit b80da72

Browse files
committed
docs: 📝 add docs for new utilities
1 parent 12f0ba3 commit b80da72

File tree

1 file changed

+82
-14
lines changed

1 file changed

+82
-14
lines changed

README.md

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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

5050
2. 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

6161
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:
6764

6865
```bash
6966
# .env
7067
NEXT_PUBLIC_FOO="foo"
7168
BAR="bar"
69+
NEXT_PUBLIC_BAZ="baz"
7270
```
7371

74-
```jsx
72+
```tsx
73+
// pages/some-page.tsx
7574
type Props = {
76-
bar: string,
75+
bar: string;
7776
};
7877

7978
export 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
99109
import { env } from 'next-runtime-env';
100110

101111
type Props = {
102-
bar: string,
112+
bar: string;
103113
};
104114

105115
export default function SomePage({ bar }: Props) {
@@ -119,6 +129,64 @@ 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): void`
170+
171+
Makes the environment variable with the 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+
##### Example
176+
177+
```js
178+
// next.config.js
179+
const { configureRuntimeEnv } = require('next-runtime-env/build/configure');
180+
const { makeEnvPublic } = require('next-runtime-env/build/make-env-public');
181+
182+
// Given that `FOO` is declared as a regular env var, not a public one. This
183+
// will make it public and available as `NEXT_PUBLIC_FOO`.
184+
makeEnvPublic('FOO');
185+
186+
// This will generate the `__ENV.js` file and include `NEXT_PUBLIC_FOO`.
187+
configureRuntimeEnv();
188+
```
189+
122190
### Maintenance 👷
123191

124192
This package is maintained and actively used by [Expatfile.tax][expatfile-site].

0 commit comments

Comments
 (0)