Skip to content

Commit 859cee3

Browse files
committed
Add env variables
1 parent e7825ff commit 859cee3

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
npm-debug.log
33
/node_modules/
44
/.next/
5+
.env

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@zeit/next-typescript": "0.1.1",
88
"classnames": "2.2.5",
99
"express": "4.16.3",
10+
"htmlescape": "1.1.1",
1011
"next": "6.0.0",
1112
"next-images": "0.10.2",
1213
"next-redux-wrapper": "2.0.0-beta.4",
@@ -33,7 +34,7 @@
3334
},
3435
"scripts": {
3536
"dev": "nodemon server.js --watch server.js --watch routes.js",
36-
"build": "next build",
37+
"build": "NODE_ENV=production next build",
3738
"start": "NODE_ENV=production node server.js"
3839
},
3940
"keywords": [],

pages/_document.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Document, { Main, Head, NextScript } from 'next/document';
2+
import { unsafe_getEnvScriptForDocument } from '../utils/env';
23

34
export default class MyDocument extends Document {
45
static async getInitialProps(ctx: any) {
@@ -60,6 +61,8 @@ export default class MyDocument extends Document {
6061
href="https://fonts.googleapis.com/css?family=Fira+Sans:200,400,700&subset=latin-ext"
6162
rel="stylesheet"
6263
/>
64+
65+
<script dangerouslySetInnerHTML={unsafe_getEnvScriptForDocument()} />
6366
</Head>
6467
<body>
6568
<Main />

pages/staticPage.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Layout from '../components/layout/Layout';
33
import './index.scss';
44
import './staticPage.scss';
55
import { AboutPage, AuthorsPage } from './staticPages';
6+
import env from '../utils/env';
67

78
type StaticPageContent = { component: React.ComponentType; title: string };
89
const pathToContent: Record<string, StaticPageContent> = {
@@ -23,6 +24,7 @@ export default class Index extends React.Component<{ asPath: string }> {
2324
return (
2425
<Layout title={content.title}>
2526
<div className="container">
27+
Process.env: {JSON.stringify(env.API_URL, null, 4)}
2628
<Component />
2729
</div>
2830
</Layout>

utils/env.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @ts-ignore
2+
const env: ProcessENV = 'undefined' !== typeof window ? (window['__ENV__'] as object) : process.env;
3+
export default env;
4+
5+
export type ProcessENV = ReturnType<typeof getEnvObjForDocument>;
6+
7+
const getEnvObjForDocument = () => {
8+
const { API_URL } = process.env;
9+
const env = { API_URL };
10+
return env;
11+
};
12+
13+
export const unsafe_getEnvScriptForDocument = () => {
14+
const env = getEnvObjForDocument();
15+
return { __html: '__ENV__ = ' + require('htmlescape')(env) };
16+
};

0 commit comments

Comments
 (0)