更新逻辑,缓存

This commit is contained in:
SunWuyuan
2025-04-05 12:19:46 +08:00
parent 8a1e44f1fa
commit eefa858078
17 changed files with 292 additions and 279 deletions
+49 -66
View File
@@ -19,86 +19,62 @@ export default defineConfig({
Layouts(),
Vue({
template: { transformAssetUrls }
}),
}),
VitePWA({
registerType: 'autoUpdate',
devOptions: {
navigateFallback: '/',
navigateFallback: 'index.html',
enabled: true,
suppressWarnings: true,
},
lang: 'zh-CN',
lang: 'zh-CN',
injectRegister: 'auto',
strategies: 'generateSW',
workbox: {
globPatterns: ['**/*.{js,css,html,png,svg,jpg,jpeg,gif,ico,woff,woff2,ttf,eot}'],
navigateFallback: '/',
globPatterns: ['*'],
navigateFallback: 'index.html',
runtimeCaching: [
{
urlPattern: /\.(?:js)$/i,
handler: 'StaleWhileRevalidate',
urlPattern: ({ url }) => url.pathname.startsWith('/assets/'),
handler: 'CacheFirst',
options: {
cacheName: 'js-cache',
cacheName: 'assets-cache',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 7 // 7 天
}
}
},
{
urlPattern: /\.(?:css)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'css-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 7 // 7 天
}
}
},
{
urlPattern: /\.(?:html)$/i,
handler: 'NetworkFirst',
options: {
cacheName: 'html-cache',
expiration: {
maxEntries: 20,
maxAgeSeconds: 60 * 60 * 24 // 1 天
}
}
},
{
urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'images-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 30 // 30 天
}
}
},
{
urlPattern: /\/cdn-cgi\/.*/i,
handler: 'NetworkFirst',
options: {
cacheName: 'cdn-cgi-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 // 1 天
maxEntries: 200,
maxAgeSeconds: 60 * 60 * 24 * 60 // 60 天
},
networkTimeoutSeconds: 10
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
// 匹配除了当前域名以外的所有请求
urlPattern: ({ url }) => url.pathname.startsWith('/pwa/'),
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'pwa-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 7 // 7 天
},
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
// 匹配除了上述规则外的所有请求
urlPattern: ({ url }) => {
return url.origin !== self.location.origin;
const path = url.pathname;
// 排除已经由其他规则处理的路径
return !(path.includes('/assets/') || path.includes('/pwa/'));
},
handler: 'NetworkFirst',
options: {
cacheName: 'external-resources',
cacheName: 'other-resources',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 // 1 天
@@ -108,12 +84,13 @@ export default defineConfig({
statuses: [0, 200]
}
}
}
},
],
additionalManifestEntries: [],
clientsClaim: true,
skipWaiting: true,
importScripts: ['sw-cache-manager.js']
importScripts: ['/sw-cache-manager.js']
},
manifest: {
name: 'Classworks作业板',
@@ -128,22 +105,22 @@ export default defineConfig({
},
icons: [
{
src: '/image/pwa-64x64.png',
src: '/pwa/image/pwa-64x64.png',
sizes: '64x64',
type: 'image/png'
},
{
src: '/image/pwa-192x192.png',
src: '/pwa/image/pwa-192x192.png',
sizes: '192x192',
type: 'image/png'
},
{
src: '/image/pwa-512x512.png',
src: '/pwa/image/pwa-512x512.png',
sizes: '512x512',
type: 'image/png'
},
{
src: '/image/maskable-icon-512x512.png',
src: '/pwa/image/maskable-icon-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'maskable'
@@ -154,9 +131,15 @@ export default defineConfig({
name: '随机点名',
short_name: '随机点名',
url: '/#random-picker',
icons: [
{
src: '/pwa/image/pwa-64x64.png',
sizes: '64x64',
type: 'image/png'
}
]
},
],
}
}),
// https://github.com/vuetifyjs/vuetify-loader/tree/master/packages/vite-plugin#readme