temp
This commit is contained in:
		
							
								
								
									
										106
									
								
								sw/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								sw/index.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,106 @@
 | 
			
		||||
<html>
 | 
			
		||||
 | 
			
		||||
<head>
 | 
			
		||||
  <title>Service Worker</title>
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body>
 | 
			
		||||
  <script>
 | 
			
		||||
    window.clearSW = () => {
 | 
			
		||||
      navigator.serviceWorker.getRegistrations().then((registrations) => {
 | 
			
		||||
        registrations.forEach((registration) => {
 | 
			
		||||
          registration.unregister();
 | 
			
		||||
        });
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  </script>
 | 
			
		||||
  <script>
 | 
			
		||||
    if ('serviceWorker' in navigator) {
 | 
			
		||||
      window.addEventListener('load', () => {
 | 
			
		||||
        navigator.serviceWorker.register('./sw.js')
 | 
			
		||||
          .then(registration => {
 | 
			
		||||
            console.log('Service Worker registered with scope:', registration.scope);
 | 
			
		||||
            // 检查是否有新版本的 Service Worker
 | 
			
		||||
            checkForNewVersion(registration);
 | 
			
		||||
          })
 | 
			
		||||
          .catch(error => {
 | 
			
		||||
            console.error('Service Worker registration failed:', error);
 | 
			
		||||
          });
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function checkForNewVersion(registration) {
 | 
			
		||||
      registration.onupdatefound = () => {
 | 
			
		||||
        const installingWorker = registration.installing;
 | 
			
		||||
        console.log('A new version is being installed:', installingWorker);
 | 
			
		||||
 | 
			
		||||
        installingWorker.onstatechange = () => {
 | 
			
		||||
          if (installingWorker.state === 'installed') {
 | 
			
		||||
            if (navigator.serviceWorker.controller) {
 | 
			
		||||
              console.log('New content is available; please refresh.');
 | 
			
		||||
              // 显示一个提示给用户,告知他们有新版本可用
 | 
			
		||||
              showUpdateNotification();
 | 
			
		||||
            } else {
 | 
			
		||||
              console.log('Content is cached for offline use.');
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        };
 | 
			
		||||
      };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function showUpdateNotification() {
 | 
			
		||||
      // 显示一个通知给用户,告知他们有新版本可用
 | 
			
		||||
      if (document.hasFocus()) {
 | 
			
		||||
        // 如果页面已经聚焦,直接显示通知
 | 
			
		||||
        alert('New content is available; please refresh.');
 | 
			
		||||
        window.location.reload();
 | 
			
		||||
      } else {
 | 
			
		||||
        // 如果页面没有聚焦,可以使用 Notification API
 | 
			
		||||
        if (Notification.permission === 'granted') {
 | 
			
		||||
          new Notification('New content is available; please refresh.');
 | 
			
		||||
        } else if (Notification.permission !== 'denied') {
 | 
			
		||||
          Notification.requestPermission().then(permission => {
 | 
			
		||||
            if (permission === 'granted') {
 | 
			
		||||
              new Notification('New content is available; please refresh.');
 | 
			
		||||
            }
 | 
			
		||||
          });
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  </script>
 | 
			
		||||
  <script>
 | 
			
		||||
    // fetch('/api/s1/sw/js/123.js')
 | 
			
		||||
    //   .then(res => res.text())
 | 
			
		||||
    //   .then(text => {
 | 
			
		||||
    //     console.log('text', text);
 | 
			
		||||
    //   });
 | 
			
		||||
    // fetch('/api/s1/sw/js/123.js', {
 | 
			
		||||
    //   method: 'POST',
 | 
			
		||||
    //   body: JSON.stringify({
 | 
			
		||||
    //     a: 1,
 | 
			
		||||
    //   }),
 | 
			
		||||
    // })
 | 
			
		||||
    //   .then(res => res.text())
 | 
			
		||||
    //   .then(text => {
 | 
			
		||||
    //     console.log('text', text);
 | 
			
		||||
    //   });
 | 
			
		||||
  </script>
 | 
			
		||||
  <script type="module">
 | 
			
		||||
    const url = new URL('/api/s1/sw/js/123.js', window.location.origin);
 | 
			
		||||
    import(url).then(res => {
 | 
			
		||||
      console.log('res', res);
 | 
			
		||||
    });
 | 
			
		||||
    // import(url).then(res => {
 | 
			
		||||
    //   console.log('res2', res);
 | 
			
		||||
    // });
 | 
			
		||||
    fetch(url).then(res => {
 | 
			
		||||
      console.log('res3', res);
 | 
			
		||||
    });
 | 
			
		||||
    fetch(url).then(res => {
 | 
			
		||||
      console.log('res4', res);
 | 
			
		||||
    });
 | 
			
		||||
  </script>
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										17
									
								
								sw/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								sw/package.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "sw",
 | 
			
		||||
  "version": "0.0.1",
 | 
			
		||||
  "description": "",
 | 
			
		||||
  "main": "index.js",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "test": "echo \"Error: no test specified\" && exit 1"
 | 
			
		||||
  },
 | 
			
		||||
  "keywords": [],
 | 
			
		||||
  "author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
 | 
			
		||||
  "license": "MIT",
 | 
			
		||||
  "packageManager": "pnpm@10.6.2",
 | 
			
		||||
  "type": "module",
 | 
			
		||||
  "devDependencies": {
 | 
			
		||||
    "vite": "^6.2.2"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								sw/public/sw.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								sw/public/sw.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
async function readStream(stream) {
 | 
			
		||||
  const reader = stream.getReader();
 | 
			
		||||
  let result = '';
 | 
			
		||||
 | 
			
		||||
  while (true) {
 | 
			
		||||
    const { done, value } = await reader.read();
 | 
			
		||||
    if (done) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    result += new TextDecoder().decode(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
self.addEventListener('fetch', async (event) => {
 | 
			
		||||
  const url = new URL(event.request.url);
 | 
			
		||||
  // Check if the request is for the specific path
 | 
			
		||||
  if (url.pathname.startsWith('/api/s1/sw/js/')) {
 | 
			
		||||
    console.log('event', event);
 | 
			
		||||
    console.log('url', url, event.request.method, 'mode', event.request.mode, event.request.destination);
 | 
			
		||||
    // const body = event.request.body;
 | 
			
		||||
    // const method = event.request.method;
 | 
			
		||||
    // const bodyText = await readStream(body);
 | 
			
		||||
    // console.log('body', bodyText);
 | 
			
		||||
    console.log('event.request', event.request.destination);
 | 
			
		||||
    event.respondWith(
 | 
			
		||||
      new Response('export const a = 1', {
 | 
			
		||||
        status: 200,
 | 
			
		||||
        headers: {
 | 
			
		||||
          'Content-Type': 'application/javascript',
 | 
			
		||||
        },
 | 
			
		||||
      }),
 | 
			
		||||
    );
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (url.pathname.startsWith('/api/s1/sw/js/')) {
 | 
			
		||||
    event.respondWith(
 | 
			
		||||
      fetch(event.request).then((response) => {
 | 
			
		||||
        // Clone the response to modify headers
 | 
			
		||||
        const newHeaders = new Headers(response.headers);
 | 
			
		||||
        newHeaders.set('Content-Type', 'application/javascript');
 | 
			
		||||
 | 
			
		||||
        // Return a new response with the modified headers
 | 
			
		||||
        return new Response(response.body, {
 | 
			
		||||
          status: response.status,
 | 
			
		||||
          statusText: response.statusText,
 | 
			
		||||
          headers: newHeaders,
 | 
			
		||||
        });
 | 
			
		||||
      }),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user