Skip to content

Commit

Permalink
Add env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed May 6, 2018
1 parent e7825ff commit 859cee3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
npm-debug.log
/node_modules/
/.next/
.env
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@zeit/next-typescript": "0.1.1",
"classnames": "2.2.5",
"express": "4.16.3",
"htmlescape": "1.1.1",
"next": "6.0.0",
"next-images": "0.10.2",
"next-redux-wrapper": "2.0.0-beta.4",
Expand All @@ -33,7 +34,7 @@
},
"scripts": {
"dev": "nodemon server.js --watch server.js --watch routes.js",
"build": "next build",
"build": "NODE_ENV=production next build",
"start": "NODE_ENV=production node server.js"
},
"keywords": [],
Expand Down
3 changes: 3 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Document, { Main, Head, NextScript } from 'next/document';
import { unsafe_getEnvScriptForDocument } from '../utils/env';

export default class MyDocument extends Document {
static async getInitialProps(ctx: any) {
Expand Down Expand Up @@ -60,6 +61,8 @@ export default class MyDocument extends Document {
href="https://fonts.googleapis.com/css?family=Fira+Sans:200,400,700&subset=latin-ext"
rel="stylesheet"
/>

<script dangerouslySetInnerHTML={unsafe_getEnvScriptForDocument()} />
</Head>
<body>
<Main />
Expand Down
2 changes: 2 additions & 0 deletions pages/staticPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Layout from '../components/layout/Layout';
import './index.scss';
import './staticPage.scss';
import { AboutPage, AuthorsPage } from './staticPages';
import env from '../utils/env';

type StaticPageContent = { component: React.ComponentType; title: string };
const pathToContent: Record<string, StaticPageContent> = {
Expand All @@ -23,6 +24,7 @@ export default class Index extends React.Component<{ asPath: string }> {
return (
<Layout title={content.title}>
<div className="container">
Process.env: {JSON.stringify(env.API_URL, null, 4)}
<Component />
</div>
</Layout>
Expand Down
16 changes: 16 additions & 0 deletions utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-ignore
const env: ProcessENV = 'undefined' !== typeof window ? (window['__ENV__'] as object) : process.env;
export default env;

export type ProcessENV = ReturnType<typeof getEnvObjForDocument>;

const getEnvObjForDocument = () => {
const { API_URL } = process.env;
const env = { API_URL };
return env;
};

export const unsafe_getEnvScriptForDocument = () => {
const env = getEnvObjForDocument();
return { __html: '__ENV__ = ' + require('htmlescape')(env) };
};

0 comments on commit 859cee3

Please sign in to comment.