improved meta processing and support seo tags
This commit is contained in:
@@ -3,6 +3,7 @@ import { createRouter, createWebHistory } from "vue-router";
|
|||||||
import { api } from "../helpers/api";
|
import { api } from "../helpers/api";
|
||||||
import { useApplicationStore } from "../store/ApplicationStore";
|
import { useApplicationStore } from "../store/ApplicationStore";
|
||||||
import { useProgressStore } from "../store/ProgressStore";
|
import { useProgressStore } from "../store/ProgressStore";
|
||||||
|
import { updateSEOTags } from "../helpers/seo";
|
||||||
|
|
||||||
export const routes = [
|
export const routes = [
|
||||||
{
|
{
|
||||||
@@ -10,6 +11,8 @@ export const routes = [
|
|||||||
name: "home",
|
name: "home",
|
||||||
meta: {
|
meta: {
|
||||||
title: "Home",
|
title: "Home",
|
||||||
|
description:
|
||||||
|
"STEMMechanics, a family-run company based in Cairns, Queensland, creates fantastic STEM-focused programs and activities that are both entertaining and educational.",
|
||||||
},
|
},
|
||||||
component: () => import("@/views/Home.vue"),
|
component: () => import("@/views/Home.vue"),
|
||||||
},
|
},
|
||||||
@@ -397,50 +400,76 @@ router.beforeEach(async (to, from, next) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Document Title
|
const getMetaValue = (tag, defaultValue = "") => {
|
||||||
const nearestWithTitle = to.matched
|
const getMeta = (obj, tag) => {
|
||||||
.slice()
|
const tagHierarchy = tag.split(".");
|
||||||
.reverse()
|
|
||||||
.find((r) => r.meta && r.meta.title);
|
|
||||||
const previousNearestWithMeta = from.matched
|
|
||||||
.slice()
|
|
||||||
.reverse()
|
|
||||||
.find((r) => r.meta && r.meta.metaTags);
|
|
||||||
|
|
||||||
let title = "";
|
const nearestWithMeta = obj.matched
|
||||||
if (nearestWithTitle) {
|
.slice()
|
||||||
title = nearestWithTitle.meta.title;
|
.reverse()
|
||||||
} else if (previousNearestWithMeta) {
|
.reduce(
|
||||||
title = previousNearestWithMeta.meta.title;
|
(acc, r) => acc || (r.meta && r.meta[tagHierarchy[0]]),
|
||||||
}
|
null
|
||||||
|
);
|
||||||
|
if (nearestWithMeta) {
|
||||||
|
let result = nearestWithMeta;
|
||||||
|
for (let i = 1; i < tagHierarchy.length; i++) {
|
||||||
|
result = result[tagHierarchy[i]];
|
||||||
|
if (!result) break;
|
||||||
|
}
|
||||||
|
if (result !== undefined) return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (title != "") {
|
return null;
|
||||||
document.title = "STEMMechanics | " + title;
|
};
|
||||||
}
|
|
||||||
|
const nearestMeta = getMeta(to, tag);
|
||||||
|
if (nearestMeta == null) {
|
||||||
|
const previousMeta = getMeta(from, tag);
|
||||||
|
if (previousMeta == null) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return previousMeta;
|
||||||
|
}
|
||||||
|
return nearestMeta;
|
||||||
|
};
|
||||||
|
|
||||||
|
updateSEOTags({
|
||||||
|
title: getMetaValue("title"),
|
||||||
|
description: getMetaValue("description"),
|
||||||
|
keywords: getMetaValue("keywords", []),
|
||||||
|
robots: {
|
||||||
|
index: getMetaValue("robots.index", true),
|
||||||
|
follow: getMetaValue("robots.follow", true),
|
||||||
|
},
|
||||||
|
url: getMetaValue("url", to.path),
|
||||||
|
image: getMetaValue("image", ""),
|
||||||
|
});
|
||||||
|
|
||||||
// Meta tags
|
// Meta tags
|
||||||
const nearestWithMeta = to.matched
|
// const nearestWithMeta = to.matched
|
||||||
.slice()
|
// .slice()
|
||||||
.reverse()
|
// .reverse()
|
||||||
.find((r) => r.meta && r.meta.metaTags);
|
// .find((r) => r.meta && r.meta.metaTags);
|
||||||
Array.from(document.querySelectorAll("[data-vue-router-controlled]")).map(
|
// Array.from(document.querySelectorAll("[data-vue-router-controlled]")).map(
|
||||||
(el) => el.parentNode.removeChild(el)
|
// (el) => el.parentNode.removeChild(el)
|
||||||
);
|
// );
|
||||||
if (nearestWithMeta) {
|
// if (nearestWithMeta) {
|
||||||
nearestWithMeta.meta.metaTags
|
// nearestWithMeta.meta.metaTags
|
||||||
.map((tagDef) => {
|
// .map((tagDef) => {
|
||||||
const tag = document.createElement("meta");
|
// const tag = document.createElement("meta");
|
||||||
|
|
||||||
Object.keys(tagDef).forEach((key) => {
|
// Object.keys(tagDef).forEach((key) => {
|
||||||
tag.setAttribute(key, tagDef[key]);
|
// tag.setAttribute(key, tagDef[key]);
|
||||||
});
|
// });
|
||||||
|
|
||||||
tag.setAttribute("data-vue-router-controlled", "");
|
// tag.setAttribute("data-vue-router-controlled", "");
|
||||||
|
|
||||||
return tag;
|
// return tag;
|
||||||
})
|
// })
|
||||||
.forEach((tag) => document.head.appendChild(tag));
|
// .forEach((tag) => document.head.appendChild(tag));
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (to.meta.middleware == "authenticated" && !userStore.id) {
|
if (to.meta.middleware == "authenticated" && !userStore.id) {
|
||||||
progressStore.finish();
|
progressStore.finish();
|
||||||
|
|||||||
Reference in New Issue
Block a user