Skip to content

Commit

Permalink
feat(plugin): support runtimeConfig (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyaTura authored Jul 9, 2020
1 parent fc50e46 commit 351ea5e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
35 changes: 35 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
6 changes: 4 additions & 2 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit 351ea5e

Please sign in to comment.