unis-oa/frontend/sw.template.js

97 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* UnisOA 的离线外壳。目标只有两条,都很克制:
*
* 1. **断网点开图标不该白屏**:导航请求先走网络,网不通就回缓存里的应用外壳,
* 剩下的交给页面自己的「加载失败」状态去说;
* 2. **别把接口数据当静态资源缓存**:/api、/v1、/setup 这些一律放行不碰。
* 缓存里的待办列表看起来是"能离线办公",实际是让人对着一张过期的单子签字。
*
* 静态资源(/assets/xxx-哈希.js)走 stale-while-revalidate:哈希在文件名里,
* 换版本等于换 URL,所以旧缓存只会占地方不会给错内容。
*
* 本文件不在 public/ 里:构建时由 vite.config.ts 的 serviceWorkerPlugin 把它发成 dist/sw.js,
* 并把下面那行 CACHE 的版本换成构建时间戳——于是每次发版,还开着页面的那个人都会收到
* 一次「有新版本」,什么时候刷新由他自己决定。
*/
// __OA_SW_VERSION__ 由构建改写;手改这里也能达到同样效果(数字/字符串都行,变了就换缓存)
const CACHE = 'unisoa-static-__OA_SW_VERSION__'
const SHELL = '/'
const PRECACHE = [SHELL, '/manifest.webmanifest', '/icons/icon-192.png', '/icons/icon-512.png']
// 这些前缀是后端的,一个字节都不许进缓存
const PASS_THROUGH = ['/api', '/v1', '/setup', '/sso', '/print', '/open']
self.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(CACHE)
.then((cache) => cache.addAll(PRECACHE))
// 不 skipWaiting:正填到一半的单子不该被一次发版打断
.catch(() => undefined)
)
})
self.addEventListener('activate', (event) => {
event.waitUntil(
caches
.keys()
.then((keys) => Promise.all(keys.filter((key) => key !== CACHE).map((key) => caches.delete(key))))
.then(() => self.clients.claim())
)
})
// 页面点了「立即刷新」才接管,避免用户正在打字时被换掉页面
self.addEventListener('message', (event) => {
if (event.data === 'OA_SW_SKIP_WAITING') self.skipWaiting()
})
function shouldPassThrough(url) {
if (url.origin !== self.location.origin) return true
return PASS_THROUGH.some((prefix) => url.pathname === prefix || url.pathname.startsWith(`${prefix}/`))
}
function isStaticAsset(url) {
return url.origin === self.location.origin && (url.pathname.startsWith('/assets/') || url.pathname.startsWith('/icons/'))
}
async function handleNavigation(request) {
try {
const response = await fetch(request)
// 线上拿到的外壳顺手刷新进缓存,下次断网用的就是今天这一版
const copy = response.clone()
const cache = await caches.open(CACHE)
await cache.put(SHELL, copy)
return response
} catch (error) {
const cached = (await caches.match(SHELL)) || (await caches.match('/'))
if (cached) return cached
throw error
}
}
async function handleStatic(request) {
const cache = await caches.open(CACHE)
const cached = await cache.match(request)
const network = fetch(request)
.then(async (response) => {
if (response && response.ok) cache.put(request, response.clone())
return response
})
.catch(() => undefined)
return cached || (await network)
}
self.addEventListener('fetch', (event) => {
const request = event.request
if (request.method !== 'GET') return
const url = new URL(request.url)
if (shouldPassThrough(url)) return
if (request.mode === 'navigate') {
event.respondWith(handleNavigation(request))
return
}
if (isStaticAsset(url)) {
event.respondWith(handleStatic(request))
}
})