Skip to content

Commit 8248e60

Browse files
authored
feat(plugin): support runtimeConfig (#81)
1 parent 24bf456 commit 8248e60

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ export default {
4545
}
4646
```
4747

48+
### Runtime Config
49+
50+
You can use [runtime config](https://nuxtjs.org/guide/runtime-config) if need to use dynamic environment variables in production. Otherwise, the options will be hardcoded during the build and won't be read from `nuxt.config` anymore.
51+
52+
```js
53+
export default {
54+
buildModules: [
55+
'@nuxtjs/google-analytics'
56+
],
57+
58+
googleAnalytics: {
59+
id: 'UA-12301-2', // Used as fallback if no runtime config is provided
60+
},
61+
62+
publicRuntimeConfig: {
63+
googleAnalytics: {
64+
id: process.env.GOOGLE_ANALYTICS_ID
65+
}
66+
},
67+
}
68+
```
69+
4870
## Usage
4971

5072
Click [here](https://matteogabriele.gitbooks.io/vue-analytics/) for all options and features of vue-analytics

lib/plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import Vue from 'vue'
22
import VueAnalytics from 'vue-analytics'
33

44
export default async (ctx, inject) => {
5-
const options = <%= serialize(options) %>
5+
const runtimeConfig = ctx.$config && ctx.$config.googleAnalytics || {}
6+
const moduleOptions = <%= serialize(options) %>
7+
const options = {...moduleOptions, ...runtimeConfig}
68

79
if (typeof options.asyncID === 'function') {
810
options.id = await options.asyncID(ctx)

0 commit comments

Comments
 (0)