π A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!
Vue awesomedashboarddockerhomelabhomepagemitnodejsorganizationproductivitypwaself-hostedstartpagevue
β¨ Reliably apply page title/meta across views
Alicia Sykes93285f0f7145
diff --git a/src/App.vue b/src/App.vueindex 7f326e89..097ba656 100644--- a/src/App.vue+++ b/src/App.vue@@ -17,6 +17,7 @@ import CriticalError from '@/components/PageStrcture/CriticalError.vue';import LoadingScreen from '@/components/PageStrcture/LoadingScreen.vue';import { welcomeMsg } from '@/utils/logging/CoolConsole';import ErrorHandler from '@/utils/logging/ErrorHandler';+import { syncPageMeta } from '@/utils/PageMeta';import Keys from '@/utils/StoreMutations';import {localStorageKeys,@@ -47,6 +48,11 @@ export default {config() {this.isFetching = false;},+ /* Sync document title + description whenever route or loaded config changes */+ metaDeps: {+ handler() { syncPageMeta(this.$route, this.$store); },+ immediate: true,+ },},computed: {/* If the user has specified custom text for footer - get it */@@ -85,6 +91,10 @@ export default {subPageClassName() {return this.$store.state.currentConfigInfo?.confId || '';},+ /* Dep tuple so the metaDeps watcher tracks every reactive input */+ metaDeps() {+ return [this.$route.fullPath, this.pageInfo, this.sections];+ },topLevelStyleModifications() {const vc = this.visibleComponents;let styles = '';diff --git a/src/mixins/HomeMixin.js b/src/mixins/HomeMixin.jsindex 62d8751a..ff31df3c 100644--- a/src/mixins/HomeMixin.js+++ b/src/mixins/HomeMixin.js@@ -38,14 +38,6 @@ const HomeMixin = {async $route() {this.loadUpConfig();},- pageInfo: {- handler(newPageInfo) {- if (newPageInfo && newPageInfo.title) {- document.title = newPageInfo.title;- }- },- immediate: true,- },},async created() {this.loadUpConfig();diff --git a/src/router.js b/src/router.jsindex 337b67a5..145ce5e8 100644--- a/src/router.js+++ b/src/router.js@@ -6,7 +6,6 @@// Import vue routerimport { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';-import { nextTick } from 'vue';import { Progress } from 'rsup-progress';// Import views, that are not lazy-loaded@@ -16,7 +15,7 @@ import Home from '@/views/Home.vue';import store from '@/store';import Keys from '@/utils/StoreMutations';import { isAuthEnabled, isLoggedIn, isGuestAccessEnabled } from '@/utils/auth/Auth';-import { metaTagData, startingView as defaultStartingView, routePaths } from '@/utils/config/defaults';+import { startingView as defaultStartingView, routePaths } from '@/utils/config/defaults';import { VIEW_META } from '@/utils/config/ConfigHelpers';import ErrorHandler from '@/utils/logging/ErrorHandler';@@ -37,27 +36,19 @@ const resolveStartingView = () => {return VIEW_META[view] ? view : 'home';};-/* Returns the meta tags for each route */-const makeMetaTags = (defaultTitle) => {- const userTitle = import.meta.env.VITE_APP_TITLE || '';- const title = userTitle ? `${userTitle} | ${defaultTitle}` : defaultTitle;- return { title, metaTags: metaTagData };-};-/* Build the canonical /<view>/:page?/:section? routes for a given view + component.- * withSection=false for workspace (no single-section view yet). */-const makeViewRoutes = (basePath, viewName, component, title, withSection = true) => {- const meta = makeMetaTags(title);+ * withSection=false for workspace (no single-section view yet). Page meta is+ * owned by App.vue's watcher via PageMeta.js β routes don't carry titles. */+const makeViewRoutes = (basePath, viewName, component, withSection = true) => {const routes = [- { path: basePath, name: viewName, component, meta },- { path: `${basePath}/:page`, name: `${viewName}-page`, component, meta },+ { path: basePath, name: viewName, component },+ { path: `${basePath}/:page`, name: `${viewName}-page`, component },];if (withSection) {routes.push({path: `${basePath}/:page/:section`,name: `${viewName}-section`,component,- meta,});}return routes;@@ -79,7 +70,6 @@ const router = createRouter({path: '/',name: 'landing',component: Home,- meta: makeMetaTags('Home Page'),beforeEnter: (to, from, next) => {const view = resolveStartingView();if (!view || view === 'home') next();@@ -88,20 +78,9 @@ const router = createRouter({},},// Canonical /<view>/:page?/:section? routes for each view- ...makeViewRoutes(routePaths.home, 'home', Home, 'Home Page'),- ...makeViewRoutes(- routePaths.minimal,- 'minimal',- () => import('./views/Minimal.vue'),- 'Start Page',- ),- ...makeViewRoutes(- routePaths.workspace,- 'workspace',- () => import('./views/Workspace.vue'),- 'Workspace',- false,- ),+ ...makeViewRoutes(routePaths.home, 'home', Home),+ ...makeViewRoutes(routePaths.minimal, 'minimal', () => import('./views/Minimal.vue')),+ ...makeViewRoutes(routePaths.workspace, 'workspace', () => import('./views/Workspace.vue'), false),{ // The login pagepath: routePaths.login,name: 'login',@@ -116,19 +95,16 @@ const router = createRouter({path: routePaths.about,name: 'about', // We lazy load the About page so as to not slow down the appcomponent: () => import('./views/About.vue'),- meta: makeMetaTags('About Dashy'),},{ // The export config pagepath: routePaths.download,name: 'download',component: () => import('./views/DownloadConfig.vue'),- meta: makeMetaTags('Download Config'),},{ // Page not found, any non-defined routes will land herepath: routePaths.notFound,name: '404',component: () => import('./views/404.vue'),- meta: makeMetaTags('404 Not Found'),beforeEnter: (to, from, next) => {if (to.redirectedFrom) { // Log error, if redirected here from another routeErrorHandler(`Route not found: '${to.redirectedFrom.fullPath}'`);@@ -160,12 +136,9 @@ router.beforeEach(async (to, from, next) => {}});-/* If title is missing, then apply default page title */-router.afterEach((to) => {+/* Stop the loading progress bar once navigation settles */+router.afterEach(() => {progress.end();- nextTick(() => {- document.title = to.meta.title || 'Dashy';- });});/* Catch navigation + lazy-import failures */diff --git a/src/utils/PageMeta.js b/src/utils/PageMeta.jsnew file mode 100644index 00000000..4b9a90c9--- /dev/null+++ b/src/utils/PageMeta.js@@ -0,0 +1,65 @@+/**+ * Handles app page meta tags. Like title, description, icon, color, etc.+ */++import { makePageName, viewFromPath } from '@/utils/config/ConfigHelpers';++const FALLBACK_TITLE = 'Dashy';++export const VIEW_LABELS = { home: null, minimal: 'Minimal', workspace: 'Workspace' };++export const STATIC_ROUTE_LABELS = {+ login: 'Login',+ about: 'About',+ download: 'Download Config',+ 404: 'Not Found',+};++/* Resolve the section display name from its URL slug, or undefined if no match */+const resolveSectionName = (sectionSlug, sections) => {+ if (!sectionSlug || !Array.isArray(sections)) return undefined;+ const match = sections.find((s) => makePageName(s?.name || '') === sectionSlug);+ return match?.name;+};++/* Compute the metadata object for the current route + store */+export const computePageMeta = (route, store) => {+ const staticLabel = STATIC_ROUTE_LABELS[route?.name];+ if (staticLabel) return { title: `${staticLabel} | ${FALLBACK_TITLE}`, description: '' };++ const pageInfo = store?.getters?.pageInfo || {};+ const base = pageInfo.title || FALLBACK_TITLE;+ const view = viewFromPath(route?.path);+ const viewLabel = VIEW_LABELS[view];+ const sectionName = resolveSectionName(route?.params?.section, store?.getters?.sections);+ const title = [sectionName, base, viewLabel].filter(Boolean).join(' | ') || FALLBACK_TITLE;+ return { title, description: pageInfo.description || '' };+};++/* Upsert a <meta name="..."> tag in the document head */+const setMetaTag = (name, content) => {+ let tag = document.querySelector(`meta[name="${name}"]`);+ if (!tag) {+ tag = document.createElement('meta');+ tag.setAttribute('name', name);+ document.head.appendChild(tag);+ }+ tag.setAttribute('content', content || '');+};++/* Map of meta-field -> DOM writer. Add new entries here to extend. */+const APPLIERS = {+ title: (v) => { document.title = v || FALLBACK_TITLE; },+ description: (v) => setMetaTag('description', v),+};++/* Side-effect: push a meta object onto the DOM */+export const applyPageMeta = (meta) => {+ Object.entries(meta || {}).forEach(([key, value]) => {+ const apply = APPLIERS[key];+ if (apply) apply(value);+ });+};++/* Convenience: compute + apply in one call */+export const syncPageMeta = (route, store) => applyPageMeta(computePageMeta(route, store));diff --git a/src/utils/config/defaults.js b/src/utils/config/defaults.jsindex 4d02d846..2fd1e10f 100644--- a/src/utils/config/defaults.js+++ b/src/utils/config/defaults.js@@ -170,8 +170,6 @@ const defaults = {},/* Amount of time to show splash screen, when enabled, in milliseconds */splashScreenTime: 1000,- /* Page meta-data, rendered in the header of each view */- metaTagData: [{ name: 'description', content: "A simple static homepage for you're server" }],/* Server location of the Backup & Sync cloud function */backupEndpoint: 'https://sync-service.dashy.to',/* Available services for fetching favicon icon for user apps */@@ -363,7 +361,6 @@ export const {modalNames,topLevelConfKeys,splashScreenTime,- metaTagData,backupEndpoint,faviconApiEndpoints,iconCdns,