[go: nahoru, domu]

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

Pre-existing service worker and cleared cached storage prevents pre-caching. #247

Open
begincalendar opened this issue May 1, 2017 · 5 comments

Comments

@begincalendar
Copy link

According to this MDN docs page, specifically this paragraph:

If there is an existing service worker available, the new version is installed in the background, but not yet activated — at this point it is called the worker in waiting. It is only activated when there are no longer any pages loaded that are still using the old service worker. As soon as there are no more such pages still loaded, the new service worker activates (becoming the active worker). Activation can happen sooner using ServiceWorkerGlobalScope.skipWaiting() and existing pages can be claimed by the active worker using Clients.claim().

I was experiencing undesirable behaviour in my application, where assets were not being pre-fetched appropriately.

I tracked down the problem to the above docs paragraph, specifically that the problem arises when you have a prior service worker still registered and have cleared the browser's cache storage.
This results in assets being cached on demand, as opposed to being pre-fetched and pre-cached.

Is this intended behaviour of the plugin, or have I stumbled upon a bug?

If things are still unclear, I can whip up a small repo to reproduce the problem.

@NekR
Copy link
Owner
NekR commented May 1, 2017

I tracked down the problem to the above docs paragraph, specifically that the problem arises when you have a prior service worker still registered and have cleared the browser's cache storage.

Why would you do that? Anyway, when new SW activates (when you close all tabs) it will pre-cache all assets and you will have your cache full again.

Is this intended behaviour of the plugin, or have I stumbled upon a bug?

If user deletes their cache manually I don't see why offline-plugin should be trying to recover from that.

@begincalendar
Copy link
Author

Why would you do that?

Sometimes people clear their browsing history and/or other data their browser has stored (e.g. cache storage).
Maybe they haven't even cleared the cache storage by will and the environment that they have operated the browser in, has done it automatically for them (e.g. on a library computer or something).

Anyway, when new SW activates (when you close all tabs) it will pre-cache all assets and you will have your cache full again.

I tried clearing the cache and then re-opening the browser, but it does not do what you describe.
I believe the reason is because of what the MDN docs say about the new service worker getting stuck in a waiting state, due to the prior service worker.

If user deletes their cache manually I don't see why offline-plugin should be trying to recover from that.

If we are to develop websites/webpages and we don't have total control over users' browsers (e.g. to prevent them from clearing their caches), then why would we not want to accommodate for that?

@NekR
Copy link
Owner
NekR commented May 2, 2017

I believe the reason is because of what the MDN docs say about the new service worker getting stuck in a waiting state, due to the prior service worker.

You're somehow misreading it. All tabs closed make new SW leave waiting state and became active. Here more details. Of course it will happen if you have build new SW, i.e. with a new version. If not, it's still the same ServiceWorker.

@begincalendar
Copy link
Author

Thanks for helping me understand service workers more.

If we take this scenario:

  1. User visits website, never having visited before.
  2. Service worker gets downloaded.
  3. Service worker receives "install" event and runs "install" event handler.
  4. Service worker receives "activate" event and runs "activate" event handler.
  5. User performs action which fires a "fetch" event in the service worker and because the assets have been pre-cached, the cache is used, not the network (assuming a cache-first strategy).
  6. The user clears their browser storage and closes all tabs of the website, but they don't unregister/destroy the service worker.
  7. User visits the website again, but the the "install" and "activate" events (of the service worker) do not fire, only the "fetch" event

This happens because the previous service worker is still registered and has already been installed and activated.

Would you agree that the scenario above is possible?

If so, would you be interested in trying to address it, specifically re-pre-caching via another means?

E.g. In the "fetch" event handler (of the service worker), maybe have a condition like so:

if (this fetch request is for the root path AND there are no cache entries in the cache) {
  run the pre-cache process/code again
}

What do you think?

@NekR
Copy link
Owner
NekR commented May 3, 2017

Would you agree that the scenario above is possible?

Yes, I agree with that.

If so, would you be interested in trying to address it, specifically re-pre-caching via another means?

Yep.

What do you think?

I think it would be better as:

if (this fetch request is for an assets in the main cache AND there are no cache entries in the cache) {
  run the pre-cache process/code again
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants