[web] [fix] Cache resource data only if the fetching succeed (#103816)

This commit is contained in:
Dacian Florea
2022-05-25 02:23:06 +03:00
committed by GitHub
parent f2e6d4782c
commit 680bc17cde
5 changed files with 232 additions and 28 deletions

View File

@@ -134,9 +134,11 @@ self.addEventListener("fetch", (event) => {
.then((cache) => {
return cache.match(event.request).then((response) => {
// Either respond with the cached resource, or perform a fetch and
// lazily populate the cache.
// lazily populate the cache only if the resource was successfully fetched.
return response || fetch(event.request).then((response) => {
cache.put(event.request, response.clone());
if (response && Boolean(response.ok)) {
cache.put(event.request, response.clone());
}
return response;
});
})