Skip to content

Commit b86b509

Browse files
Fix Service Worker caching error for unsupported URL schemes
- Added isCacheableRequest() helper to validate HTTP/HTTPS protocols - Prevents caching of chrome-extension://, moz-extension://, file://, and other unsupported schemes - Resolves console error: 'Failed to execute put on Cache: Request scheme unsupported' - Improves PWA reliability by filtering requests before cache operations
1 parent da12f94 commit b86b509

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

sw.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ self.addEventListener("activate", function (event) {
3636
event.waitUntil(self.clients.claim());
3737
});
3838

39+
// Helper function to check if a request can be cached
40+
function isCacheableRequest(request) {
41+
const url = new URL(request.url);
42+
// Only cache HTTP and HTTPS requests
43+
return url.protocol === "http:" || url.protocol === "https:";
44+
}
45+
3946
function updateCache(request, response) {
47+
// Don't cache requests with unsupported schemes
48+
if (!isCacheableRequest(request)) {
49+
return Promise.resolve();
50+
}
4051
if (response.status === 206) {
4152
console.log("Partial response is unsupported for caching.");
4253
return Promise.resolve();
@@ -66,6 +77,11 @@ function fromCache(request) {
6677
self.addEventListener("fetch", function (event) {
6778
if (event.request.method !== "GET") return;
6879

80+
// Only handle cacheable requests (HTTP/HTTPS)
81+
if (!isCacheableRequest(event.request)) {
82+
return;
83+
}
84+
6985
event.respondWith(
7086
fromCache(event.request).then(
7187
function (response) {

0 commit comments

Comments
 (0)