Skip to content

Commit a129b3f

Browse files
committed
docs: 📝 write more clarity into the docs
1 parent e77f88f commit a129b3f

File tree

2 files changed

+71
-27
lines changed

2 files changed

+71
-27
lines changed

README.md

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# next-runtime-env - Runtime Environment Configuration
22

3-
[![codecov](https://codecov.io/gh/expatfile/next-runtime-env/branch/main/graph/badge.svg?token=mbGgsweFuP)](https://codecov.io/gh/expatfile/next-runtime-env)
3+
![GitHub branch checks state][build-url] [![codecov][cov-img]][cov-url]
44

55
Populates your environment at **run-time** rather than **build-time**.
66

77
- Isomorphic - Server, browser and middleware compatible.
88
- Supports static site generation.
9-
- Supports `.env`, just like [Next.js](https://nextjs.org/docs/basic-features/environment-variables).
9+
- Supports `.env`, just like [Next.js][nextjs-env-vars].
1010

11-
Runtime environment variables are used in common best-practice patterns for building modern apps deployed via Docker. A big selling point of using Docker to begin with is to be able to build one image and have it work everywhere - dev, test, staging, production, etc - and having to "bake" in environment-specific configuration at build-time is antithetical to that goal. `next-runtime-env` aims to remove this hurdle by adding support for runtime environment variables to Next.js without sacrificing static site generation support.
11+
Runtime environment variables are used in common best-practice patterns for
12+
building modern apps deployed via Docker. A big selling point of using Docker to
13+
begin with is to be able to build one image and have it work everywhere - dev,
14+
test, staging, production, etc - and having to "bake" in environment-specific
15+
configuration at build-time is antithetical to that goal. `next-runtime-env`
16+
aims to remove this hurdle by adding support for runtime environment variables
17+
to Next.js without sacrificing static site generation support.
1218

1319
### Getting started 🚀
1420

1521
1. Add the following lines to your `next.config.js`:
1622

1723
```js
18-
const {
19-
configureRuntimeEnv,
20-
} = require('next-runtime-env/build/configure-runtime-env');
24+
const { configureRuntimeEnv } = require('next-runtime-env/build/configure');
2125

2226
configureRuntimeEnv();
2327
```
2428

25-
This will generates a `__ENV.js` file that contains allow-listed environment variables that have a `NEXT_PUBLIC_` prefix.
29+
When the server starts this will generates a `__ENV.js` file in the `public`
30+
folder that contains allow-listed environment variables that have a
31+
`NEXT_PUBLIC_` prefix.
2632

2733
2. Add the following to the head section fo your `pages/_document.js`:
2834

@@ -35,43 +41,80 @@ Done!
3541

3642
### Usage 🧑‍💻
3743

38-
In the browser your variables will be available at `window.__ENV.NEXT_PUBLIC_FOO` and on the server `process.env.NEXT_PUBLIC_FOO`.
44+
In the browser your variables will be available at
45+
`window.__ENV.NEXT_PUBLIC_FOO` and on the server `process.env.NEXT_PUBLIC_FOO`.
3946

4047
#### Helper 😉
4148

4249
We have included a helper function to make retrieving a value easier:
4350

4451
```bash
4552
# .env
46-
NEXT_PUBLIC_NEXT="Next.js"
47-
NEXT_PUBLIC_CRA="Create React App"
48-
NEXT_PUBLIC_NOT_SECRET_CODE="1234"
53+
NEXT_PUBLIC_FOO="foo"
54+
BAR="bar"
55+
```
56+
57+
```jsx
58+
type Props = {
59+
bar: string,
60+
};
61+
62+
export default function SomePage({ bar }: Props) {
63+
return (
64+
<div>
65+
{window.__ENV.NEXT_PUBLIC_FOO} {bar}
66+
</div>
67+
);
68+
}
69+
70+
export const getStaticProps: GetStaticProps = async (context) => {
71+
return {
72+
props: {
73+
bar: process.env.BAR,
74+
},
75+
};
76+
};
4977
```
5078

5179
Becomes...
5280

5381
```jsx
5482
import { env } from 'next-runtime-env';
5583

56-
export default (props) => (
57-
<div>
58-
<small>
59-
Works in the browser: <b>{env('NEXT_PUBLIC_CRA')}</b>.
60-
</small>
61-
<small>
62-
Also works for server side rendering: <b>{env('NEXT_PUBLIC_NEXT')}</b>.
63-
</small>
64-
<form>
65-
<input type="hidden" defaultValue={env('NEXT_PUBLIC_NOT_SECRET_CODE')} />
66-
</form>
67-
</div>
68-
);
84+
type Props = {
85+
bar: string,
86+
};
87+
88+
export default function SomePage({ bar }: Props) {
89+
return (
90+
<div>
91+
{env('NEXT_PUBLIC_FOO')} {bar}
92+
</div>
93+
);
94+
}
95+
96+
export const getStaticProps: GetStaticProps = async (context) => {
97+
return {
98+
props: {
99+
bar: env('BAR'),
100+
},
101+
};
102+
};
69103
```
70104

71105
### Maintenance 👷
72106

73-
This package is maintained and used by [Expatfile.tax](https://expatfile.tax). The #1 US expat tax e-filing software. 🇺🇸
107+
This package is maintained and used by [Expatfile.tax][expatfile-site].
108+
The #1 US expat tax e-filing software. 🇺🇸
74109

75110
### Other work 📚
76111

77-
Big thanks to the [react-env](https://github.com/andrewmclagan/react-env) project, which inspired us. 🙏
112+
Big thanks to the [react-env][react-env-repo]
113+
project, which inspired us. 🙏
114+
115+
[build-url]: https://img.shields.io/github/checks-status/expatfile/next-runtime-env/main
116+
[cov-img]: https://codecov.io/gh/expatfile/next-runtime-env/branch/main/graph/badge.svg?token=mbGgsweFuP
117+
[cov-url]: https://codecov.io/gh/expatfile/next-runtime-env
118+
[nextjs-env-vars]: https://nextjs.org/docs/basic-features/environment-variables
119+
[react-env-repo]: https://github.com/andrewmclagan/react-env
120+
[expatfile-site]: https://expatfile.tax

src/env.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { isBrowser } from './utils/is-browser';
22

33
/**
4-
* Reads an environment variable from the server or the browser.
4+
* Reads a safe environment variable from the browser or any environment
5+
* variable from the server (process.env).
56
*/
67
export function env(key: string) {
78
if (isBrowser()) {

0 commit comments

Comments
 (0)