question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timing issue with vue-meta

See original GitHub issue

pageviewTemplate is resolved before vue-meta has synced the page components head.

Reproduce:

  1. Download Nuxt.js starter template
  2. Install analytics module and set pageviewTemplate:
// nuxt.config.js
'google-analytics': {
  id: 'UA-XXXXXX',
  debug: {
    enabled: true,
  },
  autoTracking: {
    pageviewTemplate: route => {
      return {
        page: route.path,
        title: document.title,
        location: window.location.href,
      };
    },
  },
},
  1. Setup two pages:
<!-- pages/page1.vue -->
<template>
  <div>
    <h1 class="title">Page 1</h1>

    <div class="links">
      <nuxt-link to="page1">Page 1</nuxt-link>
      <nuxt-link to="page2">Page 2</nuxt-link>
    </div>
  </div>
</template>

<script>
export default {
  head() {
    return {
      title: 'Page 1',
    };
  },
}
</script>
<!-- pages/page2.vue -->
<template>
  <div>
    <h1 class="title">Page 2</h1>

    <div class="links">
      <nuxt-link to="page1">Page 1</nuxt-link>
      <nuxt-link to="page2">Page 2</nuxt-link>
    </div>
  </div>
</template>

<script>
export default {
  head() {
    return {
      title: 'Page 2',
    };
  },
}
</script>

Expected behavior: Page 1 will track “Page 1” as title Page 2 will track “Page 2” as title

Actual behavior: Page 1 & Page 2 tracks title correctly during initial load Page 2 tracks “Page 1” when changing route from Page 1 -> Page 2 Page 1 tracks “Page 2” when changing route from Page 2 -> Page 1

Console output:

location         (&dl)   http://localhost:3000/page2
page             (&dp)   /page2
title            (&dt)   Page 1
location         (&dl)   http://localhost:3000/page1
page             (&dp)   /page1
title            (&dt)   Page 2
<div align="right">This question is available on Nuxt.js community (#c8)</div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:12
  • Comments:50 (18 by maintainers)

github_iconTop GitHub Comments

7reactions
cbodincommented, Sep 20, 2018

Cool, maybe we should open an issue in the vue-meta repo? 😃 I’ve however found a workaround for anyone stumbling upon this issue. It does require a few things however:

  1. Use the Nuxt Router Module
  2. Disable auto tracking:
      autoTracking: {
        pageviewOnLoad: false,
        page: false,
      },
  1. Add meta to all page components, and only the page components
  2. Add a mixin in the router.js file (Maybe there’s a better place to put this?)
Vue.mixin({
  mounted() {
    if (this.$metaInfo) {
      this.$ga.page({
        page: this.$router.currentRoute.path,
        title: this.$metaInfo.title,
        location: window.location.href,
      });
    }
  },
});

I’m now getting the correct url’s and titles tracked on all pages.

4reactions
manniLcommented, Mar 23, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue meta not getting updates - Stack Overflow
This late update causes SEO not be presented by the time link being shared and validate. My full meta tags code metaInfo() {...
Read more >
Server-Side Rendering (SSR) - Vue.js
Server-rendered markup doesn't need to wait until all JavaScript has been downloaded and executed to be displayed, so your user will see a...
Read more >
How Nuxt.js solves the SEO problems in Vue.js
In the case of Vue.js, the meta tags are populated at the same time the JavaScript renders the page, so the bots may...
Read more >
Vue.js SEO-Friendly SPAs: Tips, Tools & Prerender Example
It's a complete SPA that search engines might have a hard time crawling & indexing. Why is that? A single-page application adds content...
Read more >
How to use Vue 3 Meta with Vue.js 3? - Laracasts
I am trying to use vue-meta with vue3 but its not working, ... @Tjyoung having same issue it only work on title where...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found