How it works

Goal

You see the build error when you build VuePress with Vue Google Adsense package.
So, I describe how to avoid it.

Refer to the following for the error log.

$ yarn docs:build
yarn run v1.17.3
warning package.json: No license field
$ vuepress build docs
wait Extracting site metadata...
tip Apply theme @vuepress/theme-default ...
tip Apply plugin container (i.e. "vuepress-plugin-container") ...
tip Apply plugin @vuepress/register-components (i.e. "@vuepress/plugin-register-components") ...
tip Apply plugin @vuepress/active-header-links (i.e. "@vuepress/plugin-active-header-links") ...
tip Apply plugin @vuepress/search (i.e. "@vuepress/plugin-search") ...
tip Apply plugin @vuepress/nprogress (i.e. "@vuepress/plugin-nprogress") ...
tip Apply plugin @vuepress/google-analytics (i.e. "@vuepress/plugin-google-analytics") ...
tip Apply plugin @vuepress/last-updated (i.e. "@vuepress/plugin-last-updated") ...

✔ Client
  Compiled successfully in 9.44s

✔ Server
  Compiled successfully in 5.30s

wait Rendering static HTML...
Rendering page: /error Error rendering /: false
undefined
ReferenceError: window is not defined
    at Object.<anonymous> (node_modules/vue-google-adsense/dist/VueGoogleAdsense.min.js:2:231)
    at __webpack_require__ (webpack/bootstrap:25:0)
    at Module.<anonymous> (server-bundle.js:2092:28)
    at __webpack_require__ (webpack/bootstrap:25:0)
    at Object.<anonymous> (server-bundle.js:1179:18)
    at __webpack_require__ (webpack/bootstrap:25:0)
    at server-bundle.js:118:18
    at Object.<anonymous> (server-bundle.js:121:10)
    at evaluateModule (/Users/[your-name]/sample-project/node_modules/vue-server-renderer/build.dev.js:9303:21)
    at /Users/[your-name]/sample-project/node_modules/vue-server-renderer/build.dev.js:9361:18
    at new Promise (<anonymous>)
    at /Users/[your-name]/sample-project/node_modules/vue-server-renderer/build.dev.js:9353:14
    at Object.renderToString (/Users/[your-name]/sample-project/node_modules/vue-server-renderer/build.dev.js:9529:9)
    at Build.renderPage (/Users/[your-name]/sample-project/node_modules/@vuepress/core/lib/node/build/index.js:156:34)
    at Build.render (/Users/[your-name]/sample-project/node_modules/@vuepress/core/lib/node/build/index.js:94:33)
    at async App.build (/Users/[your-name]/sample-project/node_modules/@vuepress/core/lib/node/App.js:492:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
$

Steps

Create VuePress project

VuePress 1.x

$ mkdir sample-project
$ cd sample-project
$ yarn add -D vuepress
$ mkdir docs

Create below MarkDown file. (docs/README.md)

# This is an AdSense sample

<ClientOnly>
  <CallAdsense />
</ClientOnly>

Create blow Vue file. (docs/.vuepress/components/CallAdsense.vue)

Set data-ad-client and data-ad-slot with your own values.

<template>
  <div>
    <Adsense
      data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
      data-ad-slot="XXXXXXXXXX"
    />
  </div>
</template>

Add Vue Google Adsense package

Vue Google Adsense

$ yarn add -D vue-script2 vue-google-adsense

Edit JSON file. (package.json)

{
  "devDependencies": {
    "vue-google-adsense": "^1.7.0",
    "vue-script2": "^2.1.0",
    "vuepress": "^1.0.4"
  },
  "scripts": {
    "docs:dev": "vuepress dev docs",
    "docs:build": "vuepress build docs"
  }
}

Write enhanceApp.js for headless environment

App Level Enhancements

Create below JavaScript file. (docs/.vuepress/enhanceApp.js)

export default ({
  Vue,
  options,
  router,
  siteData
}) => {
  if (typeof window !== 'undefined') {
    import('vue-google-adsense')
      .then(module => {
        const Ads = module.default
        Vue.use(require('vue-script2'))
        Vue.use(Ads.Adsense)
        Vue.use(Ads.InArticleAdsense)
        Vue.use(Ads.InFeedAdsense)
      })
      .catch(e => {
        console.log(e)
      })
  }
}

Build documents

$ yarn docs:build
yarn run v1.17.3
warning package.json: No license field
$ vuepress build docs
wait Extracting site metadata...
tip Apply theme @vuepress/theme-default ...
tip Apply plugin container (i.e. "vuepress-plugin-container") ...
tip Apply plugin @vuepress/register-components (i.e. "@vuepress/plugin-register-components") ...
tip Apply plugin @vuepress/active-header-links (i.e. "@vuepress/plugin-active-header-links") ...
tip Apply plugin @vuepress/search (i.e. "@vuepress/plugin-search") ...
tip Apply plugin @vuepress/nprogress (i.e. "@vuepress/plugin-nprogress") ...

✔ Client
  Compiled successfully in 9.11s

✔ Server
  Compiled successfully in 5.65s

wait Rendering static HTML...
success Generated static files in docs/.vuepress/dist.

✨  Done in 14.55s.
$

Now, deploy docs/.vuepress/dist to your site (like Netlify) and you can see a content with Google AdSense.

Last Updated: 9/12/2019, 2:43:05 AM