1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Add runtime environment example (#3371)

This commit is contained in:
Tim Neutkens 2017-12-02 18:08:53 +01:00 committed by GitHub
parent 32eb6b7718
commit df4974eac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1 @@
export default ('undefined' !== typeof window ? window.__ENV__ : process.env)

View file

@ -0,0 +1,13 @@
{
"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"
}
}

View file

@ -0,0 +1,27 @@
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>
)
}
}

View file

@ -0,0 +1,17 @@
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>
}
}

View file

@ -0,0 +1,30 @@
[![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