From 0232de30079539e65ca9bee6c321eb694320ebc9 Mon Sep 17 00:00:00 2001 From: Joshua Graber Date: Fri, 15 Mar 2024 16:41:02 -0400 Subject: [PATCH] refactor(router): organization and comments --- client/src/router.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/client/src/router.js b/client/src/router.js index e030a645..377a1988 100644 --- a/client/src/router.js +++ b/client/src/router.js @@ -97,18 +97,22 @@ const META_PROPERTIES = [...DEFAULT_META_TAGS.keys(), 'og:url']; * @param {RouteLocationNormalized} to Vue router route location */ function refreshMetaTagsByRoute(to) { + // Get nearest matched route that has title / meta tag overrides const nearestRouteWithTitle = [...to.matched] .reverse() - .find((r) => r.meta && r.meta.title); + .find((route) => route?.meta?.title); const nearestRouteWithMeta = [...to.matched] .reverse() - .find((r) => r.meta && r.meta.metaTags); + .find((route) => route?.meta?.metaTags); + // Update document title document.title = nearestRouteWithTitle?.meta?.title ?? DEFAULT_META_TAGS.get('title'); - Array.from(document.querySelectorAll('[data-controlled-meta]')).map((el) => - el.parentNode.removeChild(el), + + // Update meta tags + Array.from(document.querySelectorAll('[data-controlled-meta]')).forEach( + (el) => el.parentNode.removeChild(el), ); META_PROPERTIES.filter((prop) => prop !== 'title')