add PWA mode

This commit is contained in:
2026-09-04 21:51:04 +08:00
parent e489fc68eb
commit 1c675b2fc2
7 changed files with 140 additions and 1 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

+15 -1
View File
@@ -8,8 +8,22 @@
<meta name="theme-color" content="#3964FE">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="Agent">
<link rel="manifest" href="/manifest.json">
<link rel="apple-touch-icon" href="/icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="192x192" href="/icons/icon-192.png">
<link rel="stylesheet" href="/css/style.css">
</head>
<script>
// 注册 Service Worker(PWA / 离线缓存)
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js').catch((err) => {
console.warn('[SW] register failed:', err);
});
});
}
</script>
</head>
<body>
<div id="app">
+3
View File
@@ -7,6 +7,9 @@
<meta name="theme-color" content="#1e3c72">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="Agent">
<link rel="manifest" href="/manifest.json">
<link rel="apple-touch-icon" href="/icons/apple-touch-icon.png">
<link rel="stylesheet" href="/css/style.css">
<style>
.login-wrap {
+31
View File
@@ -0,0 +1,31 @@
{
"name": "Agent 任务台",
"short_name": "Agent",
"description": "Agent 任务管理 Web 应用",
"start_url": "/",
"scope": "/",
"display": "standalone",
"orientation": "portrait",
"background_color": "#3964FE",
"theme_color": "#3964FE",
"lang": "zh-CN",
"icons": [
{
"src": "/icons/icon-192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "any maskable"
},
{
"src": "/icons/icon-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "any maskable"
},
{
"src": "/icons/apple-touch-icon.png",
"sizes": "180x180",
"type": "image/png"
}
]
}
+91
View File
@@ -0,0 +1,91 @@
// Agent 任务台 Service Worker
// 策略:HTML 网络优先(保证登录态/最新版本),静态资源缓存优先(图标/CSS/JS/vendor)
const CACHE_VERSION = 'agent-v1';
const STATIC_CACHE = `${CACHE_VERSION}-static`;
const RUNTIME_CACHE = `${CACHE_VERSION}-runtime`;
const PRECACHE_URLS = [
'/',
'/login.html',
'/css/style.css',
'/js/core.js',
'/js/models.js',
'/js/settings.js',
'/js/skills.js',
'/js/tasks.js',
'/vendor/marked.min.js',
'/manifest.json',
'/icons/icon-192.png',
'/icons/icon-512.png',
'/icons/apple-touch-icon.png'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(STATIC_CACHE).then((cache) =>
// addAll 任一失败会整体失败,逐个 add 更稳
Promise.all(
PRECACHE_URLS.map((url) =>
cache.add(url).catch((err) => console.warn('[SW] precache fail', url, err))
)
)
)
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys.filter((k) => !k.startsWith(CACHE_VERSION)).map((k) => caches.delete(k))
)
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const { request } = event;
if (request.method !== 'GET') return;
const url = new URL(request.url);
// 只处理同源
if (url.origin !== self.location.origin) return;
// 跳过 API/SSE,避免缓存动态数据
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/sse')) return;
// HTML/登录页:网络优先
if (request.mode === 'navigate' || request.destination === 'document') {
event.respondWith(
fetch(request)
.then((resp) => {
const copy = resp.clone();
caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy));
return resp;
})
.catch(() => caches.match(request).then((r) => r || caches.match('/')))
);
return;
}
// 静态资源:缓存优先
event.respondWith(
caches.match(request).then((cached) => {
if (cached) return cached;
return fetch(request).then((resp) => {
if (resp && resp.status === 200 && resp.type === 'basic') {
const copy = resp.clone();
caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy));
}
return resp;
}).catch(() => cached);
})
);
});
self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') self.skipWaiting();
});