feat: add PWA support
All checks were successful
Gitea Actions Demo / build-and-deploy-to-local-server (push) Successful in 2m58s
Gitea Actions Demo / deploy-to-remote-server (push) Successful in 2s

This commit is contained in:
2025-06-17 20:02:46 +08:00
parent de40dd1365
commit 6e47c0fac3
3 changed files with 53 additions and 0 deletions

21
static/manifest.json Normal file
View File

@ -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"
}
]
}

23
static/service-worker.js Normal file
View File

@ -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))
);
});

View File

@ -36,6 +36,15 @@
<meta name="naver-site-verification" content="{{ site.Params.analytics.naver.SiteVerificationTag }}"> <meta name="naver-site-verification" content="{{ site.Params.analytics.naver.SiteVerificationTag }}">
{{- end }} {{- end }}
<link rel="manifest" href="/manifest.json" />
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then(reg => console.log('Service Worker registered', reg))
.catch(err => console.log('Service Worker failed', err));
}
</script>
{{- /* Styles */}} {{- /* Styles */}}
{{- /* includes */}} {{- /* includes */}}