Comments (12)
hey, I have solved this issue in this way.
// check if request is made by chrome extensions or web page
// if request is made for web page url must contains http.
if (!(evt.request.url.indexOf('http') === 0)) return; // skip the request. if request is not made with http protocol
whole code for fetch event:
// fetch event
self.addEventListener('fetch', evt => {
// check if request is made by chrome extensions or web page
// if request is made for web page url must contains http.
if (!(evt.request.url.indexOf('http') === 0)) return; // skip the request. if request is not made with http protocol
evt.respondWith(
caches
.match(evt.request)
.then(
cacheRes =>
cacheRes ||
fetch(evt.request).then(fetchRes =>
caches.open(dynamicNames).then(cache => {
cache.put(evt.request.url, fetchRes.clone());
// check cached items size
limitCacheSize(dynamicNames, 75);
return fetchRes;
})
)
)
.catch(() => caches.match('/fallback'))
);
});
// cache size limit function
const limitCacheSize = (name, size) => {
caches.open(name).then(cache => {
cache.keys().then(keys => {
if (keys.length > size) {
cache.delete(keys[0]).then(limitCacheSize(name, size));
}
});
});
};
my service worker script: ### https://teckat.com/sw.js
from pwa-tutorial.
#1 (comment)
perfect answer
from pwa-tutorial.
self.addEventListener("fetch", event=>{
event.respondWith(caches.open(cacheName).then(cache=>{
return cache.match(event.request).then(response=>{
return response || fetch(event.request).then(networkResponse=>{
if (!(event.request.url.indexOf('chr') === 0)) cache.put(event.request, networkResponse.clone());
return networkResponse;
}
);
}
)
}
));
}
);
from pwa-tutorial.
I am also getting same error .
from pwa-tutorial.
This looks like a relevant post for this bug:
https://stackoverflow.com/questions/49157622/service-worker-typeerror-when-opening-chrome-extension
from pwa-tutorial.
@masumonline , @InSantoshMahto, @iamshaunjp Anyone who fixed this error?
from pwa-tutorial.
hey, I have solved this issue in this way.
// check if request is made by chrome extensions or web page // if request is made for web page url must contains http. if (!(evt.request.url.indexOf('http') === 0)) return; // skip the request. if request is not made with http protocol
whole code for fetch event:
// fetch event self.addEventListener('fetch', evt => { // check if request is made by chrome extensions or web page // if request is made for web page url must contains http. if (!(evt.request.url.indexOf('http') === 0)) return; // skip the request. if request is not made with http protocol evt.respondWith( caches .match(evt.request) .then( cacheRes => cacheRes || fetch(evt.request).then(fetchRes => caches.open(dynamicNames).then(cache => { cache.put(evt.request.url, fetchRes.clone()); // check cached items size limitCacheSize(dynamicNames, 75); return fetchRes; }) ) ) .catch(() => caches.match('/fallback')) ); }); // cache size limit function const limitCacheSize = (name, size) => { caches.open(name).then(cache => { cache.keys().then(keys => { if (keys.length > size) { cache.delete(keys[0]).then(limitCacheSize(name, size)); } }); }); };
my service worker script: ### https://teckat.com/sw.js
Thanks for this!!
from pwa-tutorial.
Wouldn't that also detect any https
requests as well and block all of yours? I saw the SO post and decided against because I figured it would do that. If it "works on your machine" it's probably just localhost, right?
from pwa-tutorial.
I tried adding:
caches.match(evt.request).then(cacheRes => {
return cacheRes || fetch(evt.request).then(fetchRes => {
return caches.open(dynamicCacheName).then(cache => {
+ if (new RegExp('^(?:[a-z]+:)?//', 'i').test(new URL(evt.request.url).protocol) ) return;
cache.put(evt.request.url, fetchRes.clone());
According to these links:
https://stackoverflow.com/a/19709846/4907950
madskristensen/WebEssentials.AspNetCore.ServiceWorker#66
But still no use. Are we sure that http
is the problem?
from pwa-tutorial.
So what I did was to check if the url if it doesn't starts with http or https
caches.open(version) .then(function (cache) { // here be the fix if(request.url.match("^(http|https)://")){ cache.put(request, copy); }else{ return; }
from pwa-tutorial.
I tried adding:
caches.match(evt.request).then(cacheRes => { return cacheRes || fetch(evt.request).then(fetchRes => { return caches.open(dynamicCacheName).then(cache => { + if (new RegExp('^(?:[a-z]+:)?//', 'i').test(new URL(evt.request.url).protocol) ) return; cache.put(evt.request.url, fetchRes.clone());
According to these links:
https://stackoverflow.com/a/19709846/4907950
madskristensen/WebEssentials.AspNetCore.ServiceWorker#66
But still no use. Are we sure that
http
is the problem?
According to the comments in the solution, the code is checking if the url of the request being made contains 'http'.
A browser extension is trying to make a request which is causing the error. The request from the extension does not have http in its url (example) so this line of code if (!(evt.request.url.indexOf('http') === 0)) return;
terminates the code if there is no 'http' in the request url (which is from the extension)
from pwa-tutorial.
Wouldn't that also detect any
https
requests as well and block all of yours? I saw the SO post and decided against because I figured it would do that. If it "works on your machine" it's probably just localhost, right?
No, it wont. As long as the http is at index 0, that statement returns true.
from pwa-tutorial.
Related Issues (9)
- Uncaught TypeError: Cannot read property 'innerHTML' of null
- Back Button
- Light House audit just hangs HOT 1
- It says 4 issues in materialize css file and wont load properly in live server HOT 1
- Side menu not working in iOS
- Where's the code??? HOT 2
- Error loading resources from sub-folders. HOT 2
- CacheStaticName data returning empty in fetch event
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pwa-tutorial.