/* Before a reader route draws anything — /wiki/ for an article, /wiki// for one of its language versions — put the document it names on the library's shelf. On the server that is what lets the page in view be written out in the first HTML; on the client it is what the stack of open pages grows from, and the title the page is filed under. A page that is already on the shelf comes straight off it and is re-read in the background — so an edit made elsewhere still shows up when you turn to it. */ /** How a document is filed in the tab stack: which article, which language. */ function tabMeta(article: { slug: string; language?: string; baseSlug?: string | null }) { return { lang: article.language ?? 'en', identity: article.baseSlug || article.slug } } export default defineNuxtRouteMiddleware(async (to) => { const loc = parseReaderPath(to.path) if (!loc) return /* taken before any await, while the app context is certainly at hand */ const library = useArticleLibrary() const tabs = useArticleTabs() const onShelf = loc.lang ? library.getVersion(loc.identity, loc.lang) : library.get(loc.identity) if (onShelf) { tabs.ensureTab(onShelf.slug, onShelf.title, tabMeta(onShelf)) library.revalidate(loc.identity, loc.lang ?? undefined) return } const article = loc.lang ? await library.loadVersion(loc.identity, loc.lang) : await library.load(loc.identity) if (article) tabs.ensureTab(article.slug, article.title, tabMeta(article)) })