How to disable noindex for just certain pages in vue.js?

by harrison.goodwin , in category: SEO , 4 months ago

How to disable noindex for just certain pages in vue.js?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by shanie.wisozk , 4 months ago

@harrison.goodwin 

To disable the noindex meta tag for specific pages in Vue.js, you can make use of the Vue Router and modify the meta tags dynamically.


Here's an example of how you can achieve this:

  1. Install the Vue Meta package:
1
npm install vue-meta --save


  1. Import and use the Vue Meta in your main.js or the entry file of your Vue.js project:
1
2
3
4
import Vue from 'vue';
import VueMeta from 'vue-meta';

Vue.use(VueMeta);


  1. Add meta information to your Vue Router routes:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/noindex-page',
      name: 'NoIndexPage',
      component: NoIndexPage,
      meta: {
        noindex: true, // Add this meta field to indicate noindex
      },
    },
    // Other routes...
  ],
});

router.beforeEach((to, from, next) => {
  // Update the meta tags based on the value of the "noindex" field
  if (to.meta.noindex) {
    VueMeta().metaInfo = {
      meta: [
        {
          name: 'robots',
          content: 'noindex',
        },
      ],
    };
  } else {
    VueMeta().metaInfo = {}; // Reset the meta tags for other pages
  }
  next();
});

export default router;


  1. In the Vue component of the page where you want to disable the noindex tag (e.g., NoIndexPage), you can either omit the "meta" field or set it to false:
1
2
3
export default {
  name: 'NoIndexPage',
};


Now, on your NoIndexPage, the noindex meta tag will be disabled while other pages will still have the noindex tag present.


Remember to adapt the code based on your specific project setup and routing needs.