When I visit my internal pages vue-meta
will not get updated by new page values.
Code
app.js
import VueMeta from 'vue-meta'
Vue.use(VueMeta, {
refreshOnceOnNavigation: true
})
App.vue
(main component)
export default {
metaInfo() {
return {
title: process.env.MIX_APP_NAME,
titleTemplate: `%s | ${process.env.MIX_APP_NAME}`,
meta: [
{ name: "robots", content: "index,follow" },
{
vmid: "description",
name: "description",
content:
"..........",
},
// and many more....
],
}
}
}
post.vue
(internal component)
export default {
name: "singePost",
data() {
return {
post: "",
};
},
metaInfo() {
return {
title: this.post.name, // not receiving data
meta: [
{
vmid: "description",
name: "description",
content: this.post.metas[0].description, // not receiving data
},
// others......
],
}
},
mounted() {
this.getPost();
},
methods: {
getPost() {
axios
.get("/api/articles/" + this.$route.params.slug, {
headers: {
Authorization: localStorage.getItem("access_token"),
},
})
.then((response) => {
this.post = response.data.data;
})
.catch((error) => {
//....
});
},
},
Any idea?
Update
By the time I published this question it was about not getting updated then after doing some researches I’ve and playing around with my code I’ve realized that my vue-meta gets updated but, late and it causes social network websites and SEO checkers not be able to retrieve my URLs correctly.
Clarify
- Vue-meta gets update but late
- This late update causes SEO not be presented by the time link being shared and validate.
My full meta tags code
metaInfo() {
return {
title: this.post.name,
meta: [
{
vmid: "keyword",
name: "keyword",
content: this.post.metas[0].tags,
},
{
vmid: "description",
name: "description",
content: this.post.metas[0].description,
},
// Open Graph / Facebook
{ vmid: "og:type", name: "og:type", content: "website" },
{
vmid: "og:url",
name: "og:url",
content: process.env.MIX_APP_URL + this.$router.currentRoute.fullPath,
},
{
vmid: "og:site_name",
name: "og:site_name",
content: `"${process.env.MIX_APP_NAME}"`,
},
{
vmid: "og:title",
name: "og:title",
content: this.post.name,
},
{
vmid: "og:description",
name: "og:description",
content: this.post.metas[0].description,
},
{
vmid: "og:image",
name: "og:image",
content: this.post.imagebig,
},
// Twitter
{
vmid: "twitter:card",
name: "twitter:card",
content: "summary",
},
{
vmid: "twitter:author",
name: "twitter:author",
content: "@xxxxxx",
},
{
vmid: "twitter:site",
name: "twitter:site",
content: "@xxxxxx",
},
{
vmid: "twitter:creator",
name: "twitter:creator",
content: "@xxxxxx",
},
{
vmid: "twitter:url",
name: "twitter:url",
content: process.env.MIX_APP_URL + this.$router.currentRoute.fullPath,
},
{
vmid: "twitter:title",
name: "twitter:title",
content: this.post.name,
},
{
vmid: "twitter:description",
name: "twitter:description",
content: this.post.metas[0].description,
},
{
vmid: "twitter:image",
name: "twitter:image",
content: this.post.imagebig,
},
],
};
},
Extra
-
Recently I’ve read an article that because vue-meta (Vue in general) loads based on JavaScript Social media crawlers will not cache them therefore it’s impossible to see my link details when I share them in FB or Twitter etc.
-
The suggested solution there was to use Nuxt and return meta data server side.
Questions
- I’m not sure how much #1 above is correct but its a possibility
- My app in general doesn’t use Nuxt but I just installed npm package of it so it might be worth to try (as I mentioned I never used Nuxt so if that’s the case of your helping solution I would be appreciate if you include a bit extra details into your answers about that).
4
Answers
Vue itself is client-side JS framework. When you build, your
index.html
does not have any content – only JS that generates the content when executed. Same applies to VueMeta. Problem is, when you are sharing links (FB, Twitter etc), they download linked page by using their own bot (crawler essentially) and analyze the content without executing any JS inside – so yes, they don’t see any meta generated by VueMeta…Only solution to this is to deliver fully (or partially) prerendered page containing all important information without executing JS
One way of doing so is to use Vue server side rendering – and you are right, frameworks like Nuxt use exactly that.
Generally there are two flavors:
SSR – page is rendered at the moment it is requested by the client (or bot). In most cases it requires running Node server (because Vue SSR is implemented in JS). Most prominent example of this is Nuxt.js
SSG – server side generation. Pages are generated at build time including all HTML. When loaded into the browser server returns HTML + all the JS/CSS but when it loads it’s the same Vue SPA. You don’t need Node server for that so you can host on CDN or any static hosting service like Netlify. Examples in Vue world are Gridsome, VuePress, Nuxt can do it too…
Note: there are other ways for example using headless chrome/puppeteer or services like https://prerender.io/
Nuxt
As said before, Nuxt is great but is very opinionated about how your app is structured (file based routing), how to get data etc. So switching to Nuxt can mean a complete app rewrite. On top of that it requires running NODE server which has consequences of its own (hosting).
BUT it seems to me that you are already using server – Laravel. So your best bet is probably to implement your meta rendering directly in Laravel.
UPDATE: It seems it is possible to do Vue SSR directly in Laravel
Your assumptions are correct. I’ve also spent quite some time on trying to find a solution to this very same issue a while ago. Here is what I came up with at the end of the day:
vue-meta
, for those crawlers that run JavaScript (there is no harm in it, right?).Option 1 should be clear, since you already have a similar implementation.
For option 2, here is my approach:
I picked this package for my Laravel application. It’s easy to install and register. I’m sure there are many packages for Laravel or other frameworks and languages that do the same.
I added this route at the end of my route files (
web.php
if you are using Laravel) that catches all the frontend routes requests:In
IndexController
, I first check the request to see if it’s coming from a crawler. If so, I apply the relevant meta tags. Here is a glimpse:And finally I created a template with only the
head
section (that’s the only part of html a crawler cares about) and apply the meta tags:Caveats:
Benefits:
Hope this helps! Please let me know if something is unclear.
You need to implement serverside rendering to process meta tags.
Because almost all crawler doesn’t support javascript process.
Here is the example for PHP – Laravel.
As we know vue.js is a Single Page Application. So every time it renders from one root page.
So for laravel, I configured the route as it is, and every time I return the index page with tags array and render that page in view (index page)
you can configure webpack to inject static tags
vue-cli 3 abstracts webpack config files away (generated at runtime) so in order to configure it you need to add a vue.config.js to your project root (if you don’t have one, which typically you won’t)
for instance:
(using https://github.com/jharris4/html-webpack-tags-plugin see examples in link for its concrete output)