diff --git a/app/assets/images/favicon.png b/app/assets/images/favicon.png new file mode 100644 index 0000000..0d8dd34 Binary files /dev/null and b/app/assets/images/favicon.png differ diff --git a/app/manifest.json b/app/manifest.json new file mode 100644 index 0000000..85a543b --- /dev/null +++ b/app/manifest.json @@ -0,0 +1,23 @@ +{ + "id": "/", + "name": "MD-CMS Phase 7 Test", + "short_name": "MDCMS Test", + "start_url": "./", + "display": "standalone", + "background_color": "#ffffff", + "theme_color": "#2563EB", + "icons": [ + { + "src": "assets/images/favicon.png", + "sizes": "192x192", + "type": "image/png", + "purpose": "any" + }, + { + "src": "assets/images/favicon.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "any" + } + ] +} \ No newline at end of file diff --git a/app/service-worker.js b/app/service-worker.js new file mode 100644 index 0000000..a357757 --- /dev/null +++ b/app/service-worker.js @@ -0,0 +1,57 @@ +// mdcms service worker — generated by mdcms build +const CACHE_NAME = 'mdcms-eb384247'; +const PRECACHE_URLS = [ + "index.html", + "config.yml", + "nav.yml", + "search.json", + "theme.yml", + "pages/about.md", + "pages/docs.md", + "pages/home.md", + "posts/.gitkeep", + "assets/fonts/.gitkeep", + "assets/icons/.gitkeep", + "assets/icons/arrow_drop_down.svg", + "assets/icons/arrow_right.svg", + "assets/icons/dangerous.svg", + "assets/icons/dark_mode.svg", + "assets/icons/error.svg", + "assets/icons/exclamation.svg", + "assets/icons/history.svg", + "assets/icons/info.svg", + "assets/icons/language.svg", + "assets/icons/light_mode.svg", + "assets/icons/menu.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 => { + if (event.request.method !== 'GET') return; + event.respondWith( + caches.match(event.request).then(cached => cached || fetch(event.request)) + ); +}); diff --git a/mdcms.py b/mdcms.py index af27b05..149bed9 100644 --- a/mdcms.py +++ b/mdcms.py @@ -500,21 +500,29 @@ def run_build(site_path: Path): def generate_pwa(site_path: Path, cfg: dict): """Generate manifest.json and service-worker.js when pwa: yes.""" - pwa_name = cfg.get("pwa-name", cfg.get("sitename", "MD-CMS Site")) + pwa_name = cfg.get("pwa-name", cfg.get("sitename", "MD-CMS Site")) pwa_shortname = cfg.get("pwa-shortname", pwa_name) - pwa_colour = cfg.get("pwa-colour", "#2563EB") + pwa_colour = cfg.get("pwa-colour", "#2563EB") + favicon = cfg.get("favicon", "favicon.png") + icon_src = f"assets/images/{favicon}" + + icons = [] + if (site_path / icon_src).exists(): + icons = [ + {"src": icon_src, "sizes": "192x192", "type": "image/png", "purpose": "any"}, + {"src": icon_src, "sizes": "512x512", "type": "image/png", "purpose": "any"}, + ] # manifest.json manifest = { + "id": "/", "name": pwa_name, "short_name": pwa_shortname, "start_url": "./", "display": "standalone", "background_color": "#ffffff", "theme_color": pwa_colour, - "icons": [ - {"src": "assets/images/favicon.png", "sizes": "any", "type": "image/png"} - ], + "icons": icons, } (site_path / "manifest.json").write_text( json.dumps(manifest, indent=2, ensure_ascii=False), encoding="utf-8"