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:
parent
32eb6b7718
commit
df4974eac8
1
examples/with-universal-configuration-runtime/lib/env.js
Normal file
1
examples/with-universal-configuration-runtime/lib/env.js
Normal file
|
@ -0,0 +1 @@
|
|||
export default ('undefined' !== typeof window ? window.__ENV__ : process.env)
|
13
examples/with-universal-configuration-runtime/package.json
Normal file
13
examples/with-universal-configuration-runtime/package.json
Normal 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"
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
)
|
||||
}
|
||||
}
|
17
examples/with-universal-configuration-runtime/pages/index.js
Normal file
17
examples/with-universal-configuration-runtime/pages/index.js
Normal 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>
|
||||
}
|
||||
}
|
30
examples/with-universal-configuration-runtime/readme.md
Normal file
30
examples/with-universal-configuration-runtime/readme.md
Normal 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
|
||||
|
Loading…
Reference in a new issue