Skip to content

Commit 351ea5e

Browse files
authored
feat(plugin): support runtimeConfig (nuxt-community#387)
1 parent fc50e46 commit 351ea5e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

docs/options.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@ or `axios` section in `nuxt.config.js`:
2121
}
2222
```
2323

24+
### Runtime Config
25+
26+
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.
27+
28+
Supported options:
29+
30+
- `baseURL`
31+
- `browserBaseURL`
32+
33+
**nuxt.config.js**
34+
35+
```js
36+
export default {
37+
modules: [
38+
'@nuxtjs/axios'
39+
],
40+
41+
axios: {
42+
baseURL: 'http://localhost:4000', // Used as fallback if no runtime config is provided
43+
},
44+
45+
publicRuntimeConfig: {
46+
axios: {
47+
browserBaseURL: process.env.BROWSER_BASE_URL
48+
}
49+
},
50+
51+
privateRuntimeConfig: {
52+
axios: {
53+
baseURL: process.env.BASE_URL
54+
}
55+
},
56+
}
57+
```
58+
2459
## `prefix`, `host` and `port`
2560

2661
These options are used for the default values of `baseURL` and `browserBaseURL`.

lib/plugin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ const setupProgress = (axios) => {
181181
}<% } %>
182182

183183
export default (ctx, inject) => {
184+
// runtimeConfig
185+
const runtimeConfig = ctx.$config && ctx.$config.axios || {}
184186
// baseURL
185187
const baseURL = process.browser
186-
? '<%= options.browserBaseURL || '' %>'
187-
: (process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')
188+
? (runtimeConfig.browserBaseURL || runtimeConfig.baseURL || '<%= options.browserBaseURL || '' %>')
189+
: (runtimeConfig.baseURL || process.env._AXIOS_BASE_URL_ || '<%= options.baseURL || '' %>')
188190

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

0 commit comments

Comments
 (0)