Skip to content

Commit

Permalink
feat(plugin): support runtimeConfig (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyaTura authored Jul 16, 2020
1 parent 24bf456 commit 8248e60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ export default {
}
```

### Runtime Config

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.

```js
export default {
buildModules: [
'@nuxtjs/google-analytics'
],

googleAnalytics: {
id: 'UA-12301-2', // Used as fallback if no runtime config is provided
},

publicRuntimeConfig: {
googleAnalytics: {
id: process.env.GOOGLE_ANALYTICS_ID
}
},
}
```

## Usage

Click [here](https://matteogabriele.gitbooks.io/vue-analytics/) for all options and features of vue-analytics
Expand Down
4 changes: 3 additions & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Vue from 'vue'
import VueAnalytics from 'vue-analytics'

export default async (ctx, inject) => {
const options = <%= serialize(options) %>
const runtimeConfig = ctx.$config && ctx.$config.googleAnalytics || {}
const moduleOptions = <%= serialize(options) %>
const options = {...moduleOptions, ...runtimeConfig}

if (typeof options.asyncID === 'function') {
options.id = await options.asyncID(ctx)
Expand Down

0 comments on commit 8248e60

Please sign in to comment.