Vuepress: Error rendering /: ReferenceError: document is not defined

Created on 13 May 2018  路  2Comments  路  Source: vuejs/vuepress

Hey,
I am creating a custom vuepress theme, and when I am "build"ing my app I am receiving the following error;

Error rendering /: ReferenceError: document is not defined at server-bundle.js:571:19 at Array.map (<anonymous>) at updateMetaTags (docs/.vuepress/theme/Layout.vue:35:18) at VueComponent.created (docs/.vuepress/theme/Layout.vue:18:29) at callHook (/usr/lib/node_modules/vuepress/node_modules/vue/dist/vue.runtime.common.js:2919:21) at VueComponent.Vue._init (/usr/lib/node_modules/vuepress/node_modules/vue/dist/vue.runtime.common.js:4628:5) at new VueComponent (/usr/lib/node_modules/vuepress/node_modules/vue/dist/vue.runtime.common.js:4796:12) at createComponentInstanceForVnode (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:7342:10) at renderComponentInner (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:7516:40) at renderComponent (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:7491:5) at RenderContext.renderNode (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:7407:5) at RenderContext.next (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:2428:14) at cachedWrite (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:2287:9) at renderElement (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:7645:5) at renderNode (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:7409:5) at render (/usr/lib/node_modules/vuepress/node_modules/vue-server-renderer/build.js:7753:5)

I am creating dynamic meta tags for each of my pages, when the user navigates through the app they will trigger a rebuilding of the element with the correct meta tags for that page.

In my Layout.vue file I have the following code;
````

Because "build" is server side rending my app, document is not defined on my server backend, how best to go about this.

BTW, great piece of software, I am currently in the middle of writing a blog post all about it.
Keep up the good work (thumbsup)

Most helpful comment

Try to put the call of updateMetaTags to mounted hook instead of created hook. You cannot access to document when the component is created on server side.

All 2 comments

Hi,
I'm not sure what the intention of the updated meta tags are, as they will only be updated for the users local browser and would not persist or be available beyond that.

There is no concept of document when SSR, to prevent the code throwing an error follow these rules https://vuepress.vuejs.org/guide/using-vue.html#browser-api-access-restrictions

Try to put the call of updateMetaTags to mounted hook instead of created hook. You cannot access to document when the component is created on server side.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AleksejDix picture AleksejDix  路  3Comments

gaomd picture gaomd  路  3Comments

sankincn picture sankincn  路  3Comments

lesliecdubs picture lesliecdubs  路  3Comments

harryhorton picture harryhorton  路  3Comments