


self.addEventListener("fetch", (event) => {
  const url = 'https://jsonplaceholder.typicode.com/posts/1';
  
  if (event.request.url === url) {
    event.respondWith(
      fetch(url)
        .then(response => {
          if (response.ok) {
            return response;
          }
          throw new Error('Network response was not ok.');
        })
        .catch(error => {
          console.error('Fetch error:', error);
          return new Response(JSON.stringify({ error: 'Fetch failed' }), {
            headers: { 'Content-Type': 'application/json' }
          });
        })
    );
  }
});
self.addEventListener('fetch', function(evt) {

  console.log('sw fetch() 发送的请求', evt.request.url)
})
self.addEventListener('activate', function(e) {
  // 浏览器打开网页会会掉此方法，当再次刷新页面不会回调

  console.log('[ServiceWorker] 已经被激活');
});
self.addEventListener('install', function(e) {
  // 浏览器打开网页会会掉此方法，当再次刷新页面不会回调
  console.log('[ServiceWorker] 已经安装');
});
self.addEventListener('notificationclick', function(e) {
  //
  console.log('[ServiceWorker] 通知被点击');
});
self.addEventListener('notificationclose', function(e) {
  // 通知被点击
  console.log('[ServiceWorker] 通知被关闭');
});