Skip to content

Commit

Permalink
chore: pwa
Browse files Browse the repository at this point in the history
  • Loading branch information
adams549659584 committed May 30, 2023
1 parent 538d8a4 commit 4c05bc5
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"golang.go"
]
}
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "go-proxy-bingai",
"version": "1.8.5",
"version": "1.8.6",
"private": true,
"scripts": {
"dev": "vite",
Expand Down
26 changes: 24 additions & 2 deletions frontend/src/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ declare let self: ServiceWorkerGlobalScope;
const CACHE_NAME_PREFIX = 'BingAI';

self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') self.skipWaiting();
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});

// self.__WB_MANIFEST is default injection point
Expand All @@ -21,7 +23,12 @@ cleanupOutdatedCaches();
registerRoute(new NavigationRoute(createHandlerBoundToURL('./index.html')));

registerRoute(
({ request }) => request.destination === 'style' || request.destination === 'manifest' || request.destination === 'script' || request.destination === 'worker',
({ request, url }) => {
if (url.pathname.includes('/sw.js')) {
return false;
}
return request.destination === 'style' || request.destination === 'manifest' || request.destination === 'script' || request.destination === 'worker';
},
new StaleWhileRevalidate({
cacheName: `${CACHE_NAME_PREFIX}-assets`,
plugins: [new CacheableResponsePlugin({ statuses: [200] })],
Expand All @@ -44,3 +51,18 @@ registerRoute(
],
})
);

self.addEventListener('activate', async (ev) => {
const cacheKeys = await caches.keys();
for (const cacheKey of cacheKeys) {
// 删除旧缓存
if (!cacheKey.startsWith('workbox') && !cacheKey.startsWith(CACHE_NAME_PREFIX)) {
await caches.delete(cacheKey);
console.log(`del cache : `, cacheKey);
}
}
});

self.addEventListener('install', (ev) => {
self.skipWaiting();
});
39 changes: 3 additions & 36 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const __APP_INFO__ = {

const initPwaOptions = (env: Record<string, string>) => {
const pwaOptions: Partial<VitePWAOptions> = {
// srcDir: 'src',
// filename: 'sw.ts',
srcDir: 'src',
filename: 'sw.ts',
includeAssets: ['img/logo.svg'],
manifest: {
name: 'BingAI',
Expand Down Expand Up @@ -45,7 +45,7 @@ const initPwaOptions = (env: Record<string, string>) => {
// enabled: true,
// type: 'module',
// },
// strategies: 'injectManifest',
strategies: 'injectManifest',
// workbox: {
// cleanupOutdatedCaches: true,
// clientsClaim: true,
Expand All @@ -54,39 +54,6 @@ const initPwaOptions = (env: Record<string, string>) => {
// 取消注册服务工作进程
// selfDestroying: true,
registerType: 'autoUpdate',
workbox: {
globPatterns: ['**/*.{js,css,html,ico,png,svg}'],
runtimeCaching: [
{
urlPattern: /(.*?)\.(js|css|ts)/, // js /css /ts静态资源缓存
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'BingAI-assets',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 7,
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: /(.*?)\.(png|jpe?g|svg|gif|bmp|psd|tiff|tga|eps|ico)/, // 图片缓存
handler: 'CacheFirst',
options: {
cacheName: 'BingAI-images',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 7,
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
},
};
return pwaOptions;
};
Expand Down
4 changes: 2 additions & 2 deletions web/assets/index-5d6cbc08.js → web/assets/index-199bf4d7.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<script src="/web/js/bing/chat/global.js"></script>
<script src="/web/js/bing/chat/amd.js"></script>
<script src="/web/js/bing/chat/config.js"></script>
<script type="module" crossorigin src="/web/assets/index-36027b8d.js"></script>
<script type="module" crossorigin src="/web/assets/index-3378e00b.js"></script>
<link rel="stylesheet" href="/web/assets/index-4b631cfd.css">
<link rel="manifest" href="/web/manifest.webmanifest"><script id="vite-plugin-pwa:register-sw" src="/web/registerSW.js"></script></head>

Expand Down
3 changes: 2 additions & 1 deletion web/sw.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion web/workbox-9d4bbdea.js

This file was deleted.

1 comment on commit 4c05bc5

@vercel
Copy link

@vercel vercel bot commented on 4c05bc5 May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.