fix: hidden categories now auto-switch to default instead of showing error

Two bugs fixed in navigateTo:

1. When a category has visibilityifnocontent: hidden (default) and the
   current page has no variant for it, the renderer now silently switches
   to the default category before fetching. Previously the category stayed
   active (kept visible in the selector via the activeCategory guard),
   the fetch failed, and an error page was shown.

2. The offline message stored in localStorage was shown for any failed
   fetch, not just genuine offline situations. Now gated on !navigator.onLine
   so missing pages always show pagenotfoundmessage instead.
This commit is contained in:
Claude 2026-05-21 15:27:10 +00:00
parent 5f3175497b
commit 4c8ca31651
No known key found for this signature in database

View file

@ -2653,6 +2653,24 @@ function fmtDatetime(dtStr) {
const contentEl = document.getElementById('pageContent');
highlightNav(file);
// If the active category is "hidden" (no notfoundmessage, not visibilityifnocontent:visible)
// and this page has no variant for it, silently switch to the default category instead of
// showing an error.
if (categoriesUse && activeCategory !== defaultCategoryCode && file !== defaultPage()) {
const cat = categoriesByCode[activeCategory];
const isHidden = cat && !cat.notfoundmessage && cat.visibilityifnocontent !== 'visible';
if (isHidden) {
const pageEntry = navData.find(p => p.file === file);
const hasVariant = !pageEntry || pageEntry.uncategorized
|| !(pageEntry.variants && pageEntry.variants.length)
|| pageEntry.variants.includes(activeCategory);
if (!hasVariant) {
setActiveCategory(defaultCategoryCode);
return;
}
}
}
// Build a clean URL: keep origin + path, set ?cat only when non-default, set hash to conceptual file
const u = new URL(window.location);
if (categoriesUse && activeCategory && activeCategory !== defaultCategoryCode) {
@ -2667,7 +2685,7 @@ function fmtDatetime(dtStr) {
const result = await fetchPageFile(file);
if (!result.ok) {
const offlineMsg = localStorage.getItem('mdcms-offline');
const offlineMsg = !navigator.onLine && localStorage.getItem('mdcms-offline');
const bodyMsg = offlineMsg
? `<p>${offlineMsg}</p>`
: `<p>${pageNotFoundMessage()}</p>`;