From 6e47c0fac3fca3a228b9c42f56425fe9642a3ba2 Mon Sep 17 00:00:00 2001 From: changsongd Date: Tue, 17 Jun 2025 20:02:46 +0800 Subject: [PATCH] feat: add PWA support --- static/manifest.json | 21 ++++++++++++++++++++ static/service-worker.js | 23 ++++++++++++++++++++++ themes/PaperMod/layouts/partials/head.html | 9 +++++++++ 3 files changed, 53 insertions(+) create mode 100644 static/manifest.json create mode 100644 static/service-worker.js diff --git a/static/manifest.json b/static/manifest.json new file mode 100644 index 0000000..2d5d17b --- /dev/null +++ b/static/manifest.json @@ -0,0 +1,21 @@ +{ + "name": "技术文章", + "short_name": "技术文章", + "start_url": "/", + "display": "standalone", + "background_color": "#ffffff", + "theme_color": "#317EFB", + "icons": [ + { + "src": "/images/website_icon.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "/images/website_icon.png", + "type": "image/png", + "sizes": "512x512" + } + ] +} + \ No newline at end of file diff --git a/static/service-worker.js b/static/service-worker.js new file mode 100644 index 0000000..9195e2d --- /dev/null +++ b/static/service-worker.js @@ -0,0 +1,23 @@ +const cacheName = 'v1'; + +const cacheAssets = [ + '/', + '/index.html', + '/style.css', + // 可以添加其他需要缓存的资源路径 +]; + +self.addEventListener('install', e => { + e.waitUntil( + caches.open(cacheName).then(cache => { + console.log('Caching files...'); + return cache.addAll(cacheAssets); + }) + ); +}); + +self.addEventListener('fetch', e => { + e.respondWith( + fetch(e.request).catch(() => caches.match(e.request)) + ); +}); diff --git a/themes/PaperMod/layouts/partials/head.html b/themes/PaperMod/layouts/partials/head.html index ae6450a..0676260 100644 --- a/themes/PaperMod/layouts/partials/head.html +++ b/themes/PaperMod/layouts/partials/head.html @@ -36,6 +36,15 @@ {{- end }} + + + {{- /* Styles */}} {{- /* includes */}}