vue-sw-updater 0.0.3 → 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.
- package/dist/service-worker.js +34 -17
- package/dist/vue-sw-updater.umd.js +4827 -4827
- package/dist/vue-sw-updater.umd.js.map +1 -1
- package/dist/vue-sw-updater.umd.min.js +3 -3
- package/dist/vue-sw-updater.umd.min.js.map +1 -1
- package/package.json +2 -3
- package/dist/vue-sw-updater.common.js +0 -15543
- package/dist/vue-sw-updater.common.js.map +0 -1
package/dist/service-worker.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
107
|
-
|
|
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((
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
124
|
-
|
|
135
|
+
|
|
136
|
+
if (cachedResponse) {
|
|
137
|
+
event.waitUntil(fetchAndUpdate.catch(() => {}));
|
|
138
|
+
return cachedResponse;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return fetchAndUpdate;
|
|
125
142
|
})
|
|
126
143
|
);
|
|
127
144
|
});
|