lex-xin 3 months ago
parent
commit
5f00566982
4 changed files with 65 additions and 68 deletions
  1. 52 39
      dev-dist/sw.js
  2. 1 1
      public/version.json
  3. 7 0
      src/App.tsx
  4. 5 28
      vite.config.ts

+ 52 - 39
dev-dist/sw.js

@@ -20,23 +20,21 @@ if (!self.define) {
   let nextDefineUri;
 
   const singleRequire = (uri, parentUri) => {
-    uri = new URL(uri + ".js", parentUri).href;
-    return registry[uri] || (
-      
-        new Promise(resolve => {
-          if ("document" in self) {
-            const script = document.createElement("script");
-            script.src = uri;
-            script.onload = resolve;
-            document.head.appendChild(script);
-          } else {
-            nextDefineUri = uri;
-            importScripts(uri);
-            resolve();
-          }
-        })
-      
-      .then(() => {
+    uri = new URL(uri + '.js', parentUri).href;
+    return (
+      registry[uri] ||
+      new Promise(resolve => {
+        if ('document' in self) {
+          const script = document.createElement('script');
+          script.src = uri;
+          script.onload = resolve;
+          document.head.appendChild(script);
+        } else {
+          nextDefineUri = uri;
+          importScripts(uri);
+          resolve();
+        }
+      }).then(() => {
         let promise = registry[uri];
         if (!promise) {
           throw new Error(`Module ${uri} didn’t register its module`);
@@ -47,7 +45,10 @@ if (!self.define) {
   };
 
   self.define = (depsNames, factory) => {
-    const uri = nextDefineUri || ("document" in self ? document.currentScript.src : "") || location.href;
+    const uri =
+      nextDefineUri ||
+      ('document' in self ? document.currentScript.src : '') ||
+      location.href;
     if (registry[uri]) {
       // Module is already loading or loaded.
       return;
@@ -59,15 +60,16 @@ if (!self.define) {
       exports,
       require
     };
-    registry[uri] = Promise.all(depsNames.map(
-      depName => specialDeps[depName] || require(depName)
-    )).then(deps => {
+    registry[uri] = Promise.all(
+      depsNames.map(depName => specialDeps[depName] || require(depName))
+    ).then(deps => {
       factory(...deps);
       return exports;
     });
   };
 }
-define(['./workbox-88bf3160'], (function (workbox) { 'use strict';
+define(['./workbox-88bf3160'], function (workbox) {
+  'use strict';
 
   self.skipWaiting();
   workbox.clientsClaim();
@@ -77,22 +79,33 @@ define(['./workbox-88bf3160'], (function (workbox) { 'use strict';
    * requests for URLs in the manifest.
    * See https://goo.gl/S9QRab
    */
-  workbox.precacheAndRoute([{
-    "url": "registerSW.js",
-    "revision": "3ca0b8505b4bec776b69afdba2768812"
-  }, {
-    "url": "index.html",
-    "revision": "0.e2ao6l3d16"
-  }], {});
+  workbox.precacheAndRoute(
+    [
+      {
+        url: 'registerSW.js',
+        revision: '3ca0b8505b4bec776b69afdba2768812'
+      },
+      {
+        url: 'index.html',
+        revision: '0.e2ao6l3d16'
+      }
+    ],
+    {}
+  );
   workbox.cleanupOutdatedCaches();
-  workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
-    allowlist: [/^\/$/]
-  }));
-  workbox.registerRoute(({
-    url
-  }) => url.origin === "https://dev.kt.colexiu.com", new workbox.NetworkFirst({
-    "cacheName": "api-cache",
-    plugins: []
-  }), 'GET');
+  workbox.registerRoute(
+    new workbox.NavigationRoute(workbox.createHandlerBoundToURL('index.html'), {
+      allowlist: [/^\/$/]
+    }),
+    workbox.strategies.networkOnly()
+  );
+  workbox.routing.registerRoute(
+    new RegExp('.*.html|css'),
+    workbox.strategies.networkFirst()
+  );
+  workbox.routing.registerRoute(
+    new RegExp('|js'),
+    workbox.strategies.networkOnly()
+  );
+});
 
-}));

+ 1 - 1
public/version.json

@@ -1 +1 @@
-{"version":1736995255462}
+{"version":1736996801790}

+ 7 - 0
src/App.tsx

@@ -229,6 +229,13 @@ export default defineComponent({
         <NModal maskClosable={false} v-model:show={needRefresh.value}>
           <UpdateTips onConfirm={() => updateServiceWorker(true)} />
         </NModal>
+
+        <span style={{ display: 'none' }}>
+          {needRefresh.value ? '新内容可用,点击刷新页面' : '没有新内容'}
+        </span>
+        <span style={{ display: 'none' }}>
+          {offlineReady.value ? '应用已离线可用' : '没有离线'}
+        </span>
         {/* <NModal
           show={showModal.value}
           closeOnEsc={false}

+ 5 - 28
vite.config.ts

@@ -67,38 +67,15 @@ export default defineConfig(() => {
         registerType: 'autoUpdate',
         includeAssets: ['./logo.png'],
         workbox: {
-          skipWaiting: true,
+          skipWaiting: true, // 强制跳过等待
           clientsClaim: true,
-          runtimeCaching: [
-            {
-              urlPattern: ({ url }) =>
-                url.origin === 'https://dev.kt.colexiu.com',
-              handler: 'NetworkFirst',
-              options: {
-                cacheName: 'api-cache'
-              }
-            }
-          ]
           // runtimeCaching: [
           //   {
-          //     urlPattern: /(.*?)\.(js|css|ts|tsx|html)/, // js /css /ts静态资源缓存
-          //     handler: 'NetworkOnly',
-          //     options: {
-          //       cacheName: 'js-css-cache'
-          //     }
-          //   },
-          //   {
-          //     urlPattern: /.*\.html.*/,
-          //     handler: 'NetworkOnly',
+          //     urlPattern: ({ url }) =>
+          //       url.origin === 'https://dev.kt.colexiu.com',
+          //     handler: 'NetworkFirst',
           //     options: {
-          //       cacheName: 'wisbayar-html',
-          //       expiration: {
-          //         maxEntries: 20,
-          //         maxAgeSeconds: 24 * 60 * 60
-          //       },
-          //       cacheableResponse: {
-          //         statuses: [200]
-          //       }
+          //       cacheName: 'api-cache'
           //     }
           //   }
           // ]