skip to Main Content

I’ve been working with vue-meta npm package for updating my meta tags. But I see an issue that the value which I put inside metaInfo() is not rendered correctly in google search description.

metaInfo () {
    return {
      title: 'FAQ',
      meta: [
        {name: 'description', content: 'How can we help you?'},
        {property: 'og:title', content: 'How can we help you?'},
        {property: 'og:site_name', content: 'FAQ site'},
        {property: 'og:description', content: 'How can we help you?'}
      ]
    }
  }

If we open devtools in browser, we can see the meta tags were appended in DOM. So we have two descriptions now.

  1. The meta description which we put in index.html file.
  2. The meta description which is appended in DOM with the help of the vue-meta package.

I been reading a lot about vue-meta and it hard for me to find a solution without using SSR or Nuxt, so that we could use vue-meta without using SSR.

2

Answers


  1. Unfortunately, this kind of question needs to be asked at the beginning of a project (SSR or not) and usually leads to decide what to use.

    Good thing is that Nuxt is not that different from Vue. Since it’s just a wrapper on top of Vue, you can throw 90% of your current app in it and it should work pretty fine (had somebody do just that few months ago and it went super smoothly + was pretty fast). Nuxt don’t have a lot of drawbacks and essentially add nice things on top of Vue.

    You could also give a try to this: https://prerender.io/vue-js/ (it does support SPAs)
    But IMO, you’ll get more control and flexibility with Nuxt with SSR (either in static or server target).

    Login or Signup to reply.
  2. Yes, that’s correct you should use nuxtjs if you want meta tags. I was facing the same problem as the site was already built but client demanded he need meta tags also. So instead of migrating the whole site to nuxtjs, I decided to use index.php instead of index.html . Now I can add meta tags based on page type without migrating the whole project to nuxtjs.

    suppose your index.html after building the project page looks like

    <!DOCTYPE html>
    <html lang="">
    <head>
        <meta charset="utf-8">
        <meta content="IE=edge" http-equiv="X-UA-Compatible">
        <meta content="width=device-width,initial-scale=1" name="viewport">
        <link href="/favicon.ico" rel="icon">
        <link href="/assets/fonts/fontawesome-webfont.woff2" rel="stylesheet">
        <link href="/assets/fonts/Edustdy.whtt" rel="stylesheet">
        <link href="/assets/fonts/fontawesome-webfont.woff" rel="stylesheet">
        <link href="/assets/fonts/fontawesome-webfont3e6e.eot" rel="stylesheet">
        <title>default title</title>
    
        <link href="/css/course.1d10f9e6.css" rel="prefetch">
        <link href="/js/about.b5c0f3ce.js" rel="prefetch">
        <link href="/js/course.d8d75b7d.js" rel="prefetch">
        <link href="/css/app.203f8281.css" rel="preload" as="style">
        <link href="/css/chunk-vendors.e8860705.css" rel="preload" as="style">
        <link href="/js/app.c39400af.js" rel="preload" as="script">
        <link href="/js/chunk-vendors.4cc0d445.js" rel="preload" as="script">
        <link href="/css/chunk-vendors.e8860705.css" rel="stylesheet">
        <link href="/css/app.203f8281.css" rel="stylesheet">
    </head>
    
    <body><noscript><strong>We're sorry but this site doesn't work properly without JavaScript enabled. Please enable it to
                continue.</strong></noscript>
        <div id="app"></div>
        <script src="/js/chunk-vendors.4cc0d445.js"></script>
        <script src="/js/app.c39400af.js"></script>
    
    </body>
    
    </html>
    

    My requirement was to get the meta from API for a particular page only, so the index.php would look something like

    <!DOCTYPE html>
    <html lang="">
    
    <head>
    <?php
    
     $current_url = $_SERVER['REQUEST_URI'];
    $url = explode('/',$current_url);
    if($url[1] == 'course'){
        $response =  shell_exec("curl yourapi.com/course/find?slug=$url[2]");
        $decoded = json_decode($response ,true);
    } 
    ?>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <link rel="icon" href="/favicon.ico">
        <?php 
        if(!$decoded['courseName']){
            $decoded['courseName'] = "default title";
        }
        ?>
        <meta name="title" content='<?php echo $decoded['courseName'] ?>'>
          <meta name="description" content='<?php echo $decoded['snippet']?>'>
        <meta property="og:type" content="website">
          <meta property="og:url" content="http://yoursite.com/<?php echo $current_url?>">
          <meta property="og:title" content="<?php echo $decoded['courseName']?>">
          <meta property="og:description" content="<?php echo $decoded['snippet']?>">
          <meta property="og:image" content="<?php echo $decoded['thumbnail'] ?>">
          <meta property="og:image:width" content="800" />
    <meta property="og:image:height" content="400" />
        <meta itemprop="name">
        <meta property="og:title">
        <meta name="twitter:title">
        <meta name="description">
        <meta itemprop="description">
        <meta property="og:description">
        <meta name="twitter:description">
        <link rel="stylesheet" href="/assets/fonts/fontawesome-webfont.woff2">
        <link rel="stylesheet" href="/assets/fonts/Edustdy.whtt">
        <link rel="stylesheet" href="/assets/fonts/fontawesome-webfont.woff">
        <link rel="stylesheet" href="/assets/fonts/fontawesome-webfont3e6e.eot">
    
        <title>default title</title>
    
        <link href="/css/course.1d10f9e6.css" rel="prefetch">
        <link href="/js/about.b5c0f3ce.js" rel="prefetch">
        <link href="/js/course.d8d75b7d.js" rel="prefetch">
        <link href="/css/app.203f8281.css" rel="preload" as="style">
        <link href="/css/chunk-vendors.e8860705.css" rel="preload" as="style">
        <link href="/js/app.c39400af.js" rel="preload" as="script">
        <link href="/js/chunk-vendors.4cc0d445.js" rel="preload" as="script">
        <link href="/css/chunk-vendors.e8860705.css" rel="stylesheet">
        <link href="/css/app.203f8281.css" rel="stylesheet">
    </head>
    
    <body><noscript><strong>We're sorry but this site doesn't work properly without JavaScript enabled. Please enable it to
                continue.</strong></noscript>
        <div id="app"></div>
        <script src="/js/chunk-vendors.4cc0d445.js"></script>
        <script src="/js/app.c39400af.js"></script>
    
    </body>
    
    </html>
    

    Now meta tags will be available on google, Facebook, Whatsapp, etc but as you are using PHP there might be some security issues that you need to consider as I only did this by sending 30 minutes in PHP.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search