mdcms/app/service-worker.js
Claude a5127727f0
Enable Back button and fix clean-URL reloads
Renderer (app/index.html):
- navigateTo now pushes a history entry for user navigations (pushState),
  while the initial load, back/forward (popstate/hashchange), and category
  re-renders still replaceState. The browser Back button now returns to the
  previous page instead of leaving the site.

Service worker (mdcms.py generator + app/service-worker.js):
- Serve the cached index.html app shell for navigation requests. Reloading a
  clean URL like /section-id previously 404'd on the static host before any
  JavaScript ran; the shell fallback lets the client-side router resolve the
  path. Also makes pretty-URL reloads work offline.

https://claude.ai/code/session_018KXUwmSNMGF2UBywTChCcS
2026-06-12 07:16:12 +00:00

76 lines
2.2 KiB
JavaScript

// mdcms service worker — generated by mdcms build
const CACHE_NAME = 'mdcms-a1862733';
const PRECACHE_URLS = [
"index.html",
"config.yml",
"nav.yml",
"search.json",
"theme.yml",
"pages/about.md",
"pages/docs.md",
"pages/home.md",
"pages/tabs-accordions.md",
"posts/.gitkeep",
"assets/fonts/.gitkeep",
"assets/icons/.gitkeep",
"assets/icons/add.svg",
"assets/icons/arrow_drop_down.svg",
"assets/icons/arrow_right.svg",
"assets/icons/collapse_content.svg",
"assets/icons/dangerous.svg",
"assets/icons/dark_mode.svg",
"assets/icons/error.svg",
"assets/icons/exclamation.svg",
"assets/icons/expand_content.svg",
"assets/icons/history.svg",
"assets/icons/info.svg",
"assets/icons/keyboard_arrow_down.svg",
"assets/icons/keyboard_arrow_right.svg",
"assets/icons/keyboard_double_arrow_down.svg",
"assets/icons/keyboard_double_arrow_right.svg",
"assets/icons/language.svg",
"assets/icons/light_mode.svg",
"assets/icons/menu.svg",
"assets/icons/minimize.svg",
"assets/icons/mobile_arrow_down.svg",
"assets/icons/report.svg",
"assets/icons/search.svg",
"assets/icons/success.svg",
"assets/icons/text_compare.svg",
"assets/icons/warning.svg",
"assets/images/.gitkeep",
"assets/images/favicon.png"
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(PRECACHE_URLS))
);
self.skipWaiting();
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', event => {
const req = event.request;
if (req.method !== 'GET') return;
// App-shell routing: serve cached index.html for every navigation, including
// clean URLs like /section-id on reload. Without this the static host returns
// 404 for those paths before any JavaScript runs. Works offline too.
if (req.mode === 'navigate') {
event.respondWith(
caches.match('index.html').then(shell => shell || fetch(req))
);
return;
}
event.respondWith(
caches.match(req).then(cached => cached || fetch(req))
);
});