39 lines
924 B
TypeScript
39 lines
924 B
TypeScript
import type { OnPageTransitionEndAsync, PageContext } from "vike/types";
|
|
|
|
import { buildPublicUrl } from "@/buildPublicUrl";
|
|
import NProgress from "nprogress";
|
|
|
|
export const onPageTransitionEnd: OnPageTransitionEndAsync = async (
|
|
pageContext,
|
|
) => {
|
|
NProgress.done();
|
|
NProgress.remove();
|
|
|
|
updateCanonicalTag(pageContext);
|
|
};
|
|
|
|
const updateCanonicalTag = (pageContext: PageContext) => {
|
|
const canonicalTag = findOrCreateCanonicalTag();
|
|
const canonicalUrl = buildPublicUrl(
|
|
pageContext,
|
|
pageContext.urlParsed.pathname,
|
|
);
|
|
|
|
canonicalTag.href = canonicalUrl;
|
|
};
|
|
|
|
const findOrCreateCanonicalTag = () => {
|
|
const head = document.head;
|
|
let canonicalTag: HTMLLinkElement | null = head.querySelector(
|
|
"link[rel=canonical]",
|
|
);
|
|
if (canonicalTag) return canonicalTag;
|
|
|
|
canonicalTag = document.createElement("link");
|
|
canonicalTag.rel = "canonical";
|
|
|
|
document.head.appendChild(canonicalTag);
|
|
|
|
return canonicalTag;
|
|
};
|