Unverified Commit df4974ea authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add runtime environment example (#3371)

parent 32eb6b77
export default ('undefined' !== typeof window ? window.__ENV__ : process.env)
{
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"htmlescape": "1.1.1",
"next": "4.1.4",
"react": "16.2.0",
"react-dom": "16.2.0"
}
}
import Document_, { Head, Main, NextScript } from 'next/document'
import htmlescape from 'htmlescape'
const { API_URL } = process.env
const env = { API_URL }
export default class Document extends Document_ {
static async getInitialProps (ctx) {
const props = await Document_.getInitialProps(ctx)
return props
}
render () {
return (
<html>
<Head />
<body>
<Main />
<script
dangerouslySetInnerHTML={{ __html: '__ENV__ = ' + htmlescape(env) }}
/>
<NextScript />
</body>
</html>
)
}
}
import React from 'react'
import env from '../lib/env'
const {API_URL} = env
export default class extends React.Component {
static async getInitialProps () {
// fetch(`${API_URL}/some-path`)
return {}
}
render () {
return <div>
The API_URL is {API_URL}
</div>
}
}
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-universal-configuration)
# With universal runtime configuration
## How to use
Download the example [or clone the repo](https://github.com/zeit/next.js):
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-universal-configuration-runtime
cd with-universal-configuration-runtime
```
Install it and run:
```bash
npm install
API_URL='https://example.com' npm run dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
This example show how to set custom environment variables for your application at runtime
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment