vue-sw-updater 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,7 +84,8 @@ self.addEventListener('fetch', (event) => {
84
84
  if (BLACKLIST.some(path => url.pathname.startsWith(path))) return;
85
85
 
86
86
  // HTML 页面:网络优先,失败回退到缓存 (Network First)
87
- if (event.request.mode === 'navigate' || event.request.headers.get('accept').includes('text/html')) {
87
+ const accept = event.request.headers.get('accept') || '';
88
+ if (event.request.mode === 'navigate' || accept.includes('text/html')) {
88
89
  event.respondWith(
89
90
  fetch(event.request)
90
91
  .then(response => {
@@ -103,25 +104,41 @@ self.addEventListener('fetch', (event) => {
103
104
  return;
104
105
  }
105
106
 
106
- // 静态资源:缓存优先,同时更新缓存 (Stale While Revalidate)
107
- // 或者简单的 Cache First,这里为了稳健使用 Cache First + Network Fallback
107
+ const destination = event.request.destination;
108
+ const pathname = url.pathname || '';
109
+ const isScriptOrStyle =
110
+ destination === 'script' ||
111
+ destination === 'style' ||
112
+ pathname.endsWith('.js') ||
113
+ pathname.endsWith('.css');
114
+
115
+ if (isScriptOrStyle) {
116
+ event.respondWith(
117
+ fetch(event.request)
118
+ .catch(() => caches.match(event.request))
119
+ );
120
+ return;
121
+ }
122
+
108
123
  event.respondWith(
109
- caches.match(event.request).then((response) => {
110
- if (response) {
111
- return response;
112
- }
113
- return fetch(event.request).then(networkResponse => {
114
- // 动态缓存请求成功的资源
115
- if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== 'basic') {
124
+ caches.match(event.request).then((cachedResponse) => {
125
+ const fetchAndUpdate = fetch(event.request)
126
+ .then(networkResponse => {
127
+ if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
128
+ const responseToCache = networkResponse.clone();
129
+ caches.open(CACHE_NAME).then(cache => {
130
+ cache.put(event.request, responseToCache);
131
+ });
132
+ }
116
133
  return networkResponse;
117
- }
118
- const responseToCache = networkResponse.clone();
119
- caches.open(CACHE_NAME).then(cache => {
120
- // 避免缓存过大文件或不必要文件,这里可以加过滤逻辑
121
- cache.put(event.request, responseToCache);
122
134
  });
123
- return networkResponse;
124
- });
135
+
136
+ if (cachedResponse) {
137
+ event.waitUntil(fetchAndUpdate.catch(() => {}));
138
+ return cachedResponse;
139
+ }
140
+
141
+ return fetchAndUpdate;
125
142
  })
126
143
  );
127
144
  });