Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ch11 - fetch "events.json" resolves even in offline mode #11

Open
omerok opened this issue Mar 24, 2020 · 0 comments
Open

ch11 - fetch "events.json" resolves even in offline mode #11

omerok opened this issue Mar 24, 2020 · 0 comments

Comments

@omerok
Copy link

omerok commented Mar 24, 2020

Hi,

The issue I came across occurs in Chrome and Safari, whereas works fine in Firefox(most of the time). fetch() request for events.json always resolves even in offline mode. I have added an OnlineState counter as you can see in the below code. Chrome keeps triggering this Online state even in offline mode (as can be observed from the added number of Online Requests) whereas Safari most of the time does not trigger this resolved promise at all (as can be observed from the not increasing number of Online Request). Safari only triggers Resolve function when I shift+refresh the page to delete cache and only then on my following regular refresh I can see the Resolve function triggered. So I think this is a browser file caching related problem. However, how come a failed fetch event, trigger a function inside a resolved promise in Chrome? And how come a supposedly successful fetch event not resolve in Safari?

//Added this variable before the serviceWorker fetch eventListener
var onlineRequest = 0;

// Handle requests for events JSON file
} else if (requestURL.pathname === "/events.json") {
event.respondWith(
caches.open(CACHE_NAME).then(function(cache) {
return fetch(event.request).then(function(networkResponse) {

	console.log("ONLINE: ", onlineRequest++);

      cache.put(event.request, networkResponse.clone());
      return networkResponse;
    }).catch(function() {

	console.log("OFFLINE");

      return caches.match(event.request);
    });
  })
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant