mo 1 year ago
parent
commit
e9948bc31b
5 changed files with 68 additions and 49 deletions
  1. 5 1
      dev-dist/registerSW.js
  2. 61 33
      dev-dist/sw.js
  3. BIN
      public/logo2.png
  4. 0 14
      src/App.tsx
  5. 2 1
      vite.config.ts

+ 5 - 1
dev-dist/registerSW.js

@@ -1 +1,5 @@
-if('serviceWorker' in navigator) navigator.serviceWorker.register('/dev-sw.js?dev-sw', { scope: '/', type: 'classic' })
+if ('serviceWorker' in navigator)
+  navigator.serviceWorker.register(`/dev-sw.js?dev-sw${new Date().getTime()}`, {
+    scope: '/',
+    type: 'classic'
+  });

+ 61 - 33
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-5357ef54'], (function (workbox) { 'use strict';
+define(['./workbox-5357ef54'], function (workbox) {
+  'use strict';
 
   self.skipWaiting();
   workbox.clientsClaim();
@@ -77,16 +79,42 @@ define(['./workbox-5357ef54'], (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.menbt6dkbn8"
-  }], {});
+  workbox.precacheAndRoute(
+    [
+      {
+        url: 'registerSW.js',
+        revision: '3ca0b8505b4bec776b69afdba2768812'
+      },
+      {
+        url: 'index.html',
+        revision: '0.1b6d4v58fbg'
+      }
+    ],
+    {}
+  );
   workbox.cleanupOutdatedCaches();
-  workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
-    allowlist: [/^\/$/]
-  }));
+  workbox.registerRoute(
+    new workbox.NavigationRoute(workbox.createHandlerBoundToURL('index.html'), {
+      allowlist: [/^\/$/]
+    })
+  );
+});
+
+// 监听install事件
+self.addEventListener('install', function (e) {
+  // console.log('Service Worker 状态: install');
+  console.log('install,更新');
+  self.skipWaiting();
+});
+
+// 监听activate事件
+self.addEventListener('activate', function (e) {
+  // console.log('Service Worker 状态: activate');
+  console.log('activate,更新');
+  e.waitUntil(clients.claim());
+});
 
-}));
+self.addEventListener('fetch', function (e) {
+  // 需要缓存的xhr请求
+  // console.log("fetch 事件")
+});

BIN
public/logo2.png


+ 0 - 14
src/App.tsx

@@ -83,20 +83,6 @@ export default defineComponent({
       window.open('https://www.google.cn/intl/zh-CN/chrome/');
     };
 
-    const forceSWupdate = () => {
-
-      if ('serviceWorker' in navigator) {
-
-        navigator.serviceWorker.getRegistrations().then(function (registrations) {
-          console.log(registrations, 'registrations=======>')
-          for (let registration of registrations) {
-            console.log(registration.update, 'registration==========>')
-            registration.update()
-          }
-        })
-      }
-    }
-    forceSWupdate()
     return () => (
       <>
         <NConfigProvider

+ 2 - 1
vite.config.ts

@@ -1,7 +1,7 @@
 import { defineConfig } from 'vite';
 import vue from '@vitejs/plugin-vue';
 import vueJsx from '@vitejs/plugin-vue-jsx';
-import { registerSW } from 'virtual:pwa-register';
+
 import Components from 'unplugin-vue-components/vite';
 import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
 // import viteESLint from 'vite-plugin-eslint';
@@ -66,6 +66,7 @@ export default defineConfig({
         name: '酷乐秀-音乐数字课堂',
         short_name: '音乐数字课堂',
         theme_color: '#E8EAED',
+        display: 'fullscreen',
         icons: [
           {
             src: './logo.png',