Skip to content

Commit 53177cc

Browse files
committed
fix: 状态码0视为正确响应
修复notion image 未缓存问题
1 parent 934b0a5 commit 53177cc

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

apps/web/public/sw.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ var util = {
2424
},
2525
fetchAndCache: function (request) {
2626
return fetch(request).then((response) => {
27-
// 跨域的资源直接return
28-
if (!response || response.status !== 200) {
27+
// 无响应,或非200/0状态吗的异常响应不缓存
28+
if (!response || ![200,0].includes(response.status)) {
2929
return response
3030
}
3131
util.putCache(request, response.clone())
@@ -162,19 +162,19 @@ self.addEventListener('fetch', function (e) {
162162
e.respondWith(
163163
caches
164164
.match(e.request)
165-
.then(function (response) {
165+
.then(function (cacheResponse) {
166166
const allowCache = util.checkAllowCache(e.request)
167167
if (allowCache) {
168168
// 如果没有响应或者请求内容为 doc ,则需要保持最新,重新拉取
169169
var isDoc = util.checkIsDocument(e.request);
170-
var needRefreshCache = !response || isDoc;
170+
var needRefreshCache = !cacheResponse || isDoc;
171171
if (needRefreshCache) {
172172
// 如果是 document 请求,则传入 etag 标签,比较前后的差异
173173
setTimeout(function () {
174174
util.fetchAndCache(e.request).then(function (newResponse) {
175175
// 如果是 document 请求,则比较 etag 标签,判断是否需要更新,并通知主页面,提示用户
176176
if(isDoc){
177-
var responseChanged = !util.checkSameResponse(response, newResponse)
177+
var responseChanged = !util.checkSameResponse(cacheResponse, newResponse)
178178
if(responseChanged){
179179
util.sendMessageToDocument({
180180
type: 'out_of_date',
@@ -191,8 +191,8 @@ self.addEventListener('fetch', function (e) {
191191
})
192192
},4000)
193193
}
194-
if (response) {
195-
return response
194+
if (cacheResponse) {
195+
return cacheResponse
196196
}
197197
}
198198
return fetch(e.request)

0 commit comments

Comments
 (0)