Skip to content
Permalink
Browse files
feat(plugin): support runtimeConfig (#387)
  • Loading branch information
SkyaTura committed Jul 9, 2020
1 parent fc50e46 commit 351ea5ee459eea53b9124de75ebfa7674d7ccd40
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
@@ -21,6 +21,41 @@ or `axios` section in `nuxt.config.js`:
}
```

### Runtime Config

The use of [runtime config](https://nuxtjs.org/guide/runtime-config) is mandatory in case of using environment variables in production, otherwise, the values will be hard coded during build and won't change.

Supported options:

- `baseURL`
- `browserBaseURL`

**nuxt.config.js**

```js
export default {
modules: [
'@nuxtjs/axios'
],
axios: {
baseURL: 'http://localhost:4000', // Used as fallback if no runtime config is provided
},
publicRuntimeConfig: {
axios: {
browserBaseURL: process.env.BROWSER_BASE_URL
}
},
privateRuntimeConfig: {
axios: {
baseURL: process.env.BASE_URL
}
},
}
```

## `prefix`, `host` and `port`

These options are used for the default values of `baseURL` and `browserBaseURL`.
@@ -181,10 +181,12 @@ const setupProgress = (axios) => {
}<% } %>

export default (ctx, inject) => {
// runtimeConfig
const runtimeConfig = ctx.$config && ctx.$config.axios || {}
// baseURL
const baseURL = process.browser
? '<%= options.browserBaseURL || '' %>'
: (process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')
? (runtimeConfig.browserBaseURL || runtimeConfig.baseURL || '<%= options.browserBaseURL || '' %>')
: (runtimeConfig.baseURL || process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')

// Create fresh objects for all default header scopes
// Axios creates only one which is shared across SSR requests!

0 comments on commit 351ea5e

Please sign in to comment.