fix(ui): keep the tab title until the route resolves (#2115)
Opening a page by URL showed "Page not found" in the tab while the router was still at its start location, which carries no label, so the not-found copy stood in for the page for as long as the resolution took. The title is now written only once the router has resolved the address, and the rule lives in ui/src/lib/page-title.ts so the start location's silence is tested.
This commit is contained in:
3 files changed
+47
-2
No files matched your search
+7
-2
@@ -23,6 +23,7 @@ import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar";
|
||||
import { layoutComponent, resolveLayout } from "@/layouts";
|
||||
import { TooltipProvider } from "@/components/ui/tooltip";
|
||||
import { hiddenPaths, navTree } from "@/lib/nav";
|
||||
import { pageTitle } from "@/lib/page-title";
|
||||
import { useSessionStore } from "@/stores/session";
|
||||
import { Toaster } from "vue-sonner";
|
||||
import "vue-sonner/style.css";
|
||||
@@ -62,9 +63,13 @@ watch([() => route.path, hidden], () => {
|
||||
const first = navTree(hidden.value).flatMap((node) => (node.kind === "link" ? [node.entry] : node.items))[0];
|
||||
void router.replace(first ? first.path : "/account");
|
||||
});
|
||||
watch(() => [route.path, accounts.instance], () => {
|
||||
// The tab keeps the served page's title until the router has resolved the
|
||||
// address the browser opened: writing one for the start location reads as a
|
||||
// missing page for as long as that takes.
|
||||
watch(() => [route.path, accounts.instance, routerReady.value], () => {
|
||||
const scope = route.meta.org ? "Organisation" : route.meta.settings ? "Instance" : "";
|
||||
document.title = [route.meta.label || errors.base.notFound.title, scope, "Archie", accounts.instance].filter(Boolean).join(" | ");
|
||||
const title = pageTitle(routerReady.value, typeof route.meta.label === "string" ? route.meta.label : "", scope, accounts.instance ?? "", errors.base.notFound.title);
|
||||
if (title) document.title = title;
|
||||
}, { immediate: true });
|
||||
// A page load keeps focus at the top so the skip link is the first keyboard
|
||||
// stop; later navigations move focus to the new page.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* The browser tab's title for the page on screen.
|
||||
*
|
||||
* The router is still at its start location until it has resolved the address
|
||||
* the browser opened, and that location carries no page label. Writing the
|
||||
* title then reads as a missing page for as long as the resolution takes, so an
|
||||
* unresolved route writes no title at all and the tab keeps the one the served
|
||||
* page came with.
|
||||
*/
|
||||
export function pageTitle(
|
||||
resolved: boolean,
|
||||
label: string,
|
||||
scope: string,
|
||||
instance: string,
|
||||
missing: string,
|
||||
): string {
|
||||
if (!resolved) return "";
|
||||
return [label || missing, scope, "Archie", instance].filter(Boolean).join(" | ");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import { pageTitle } from "../src/lib/page-title.ts";
|
||||
|
||||
const MISSING = "Page not found";
|
||||
|
||||
// Guards #2115: opening a dashboard page by URL showed "Page not found" in the
|
||||
// tab until the router resolved the address, because the start location has no
|
||||
// label and the not-found copy stood in for it.
|
||||
test("an unresolved route writes no title", () => {
|
||||
assert.equal(pageTitle(false, "", "", "archie", MISSING), "");
|
||||
});
|
||||
|
||||
test("a resolved route names its page", () => {
|
||||
assert.equal(pageTitle(true, "Models", "Instance", "carina", MISSING), "Models | Instance | Archie | carina");
|
||||
});
|
||||
|
||||
test("a resolved route with no page is the one that reads as not found", () => {
|
||||
assert.equal(pageTitle(true, "", "", "carina", MISSING), "Page not found | Archie | carina");
|
||||
});
|
||||
Reference in new issue
Block a user