question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fix(service-worker): handle eagerly-cached assets unexpectedly missing from the cache

See original GitHub issue

Context

It has been observed that occasionally angular.io loads an index.html file that contains (transitive) references to some JS chunks (e.g. lazy-loaded custom elements, such as the ToC module/component) which no longer exist neither on the server nor on the SW cache. When this happens, the page cannot be loaded and user sees the app-shell with a blank content area.

The frequency of these issues (which have been reported for other apps using @angular/service-worker) seem to be declining recently (see https://github.com/angular/angular/issues/28114#issuecomment-581989614), possibly due to fixes such as #32525. They still happen, though.

This is puzzling because these missing chunks are part of the same asset cache group (app-shell) and should be cached together with the index.html and main.js/runtime.js bundles that reference them. #28114 has much more info and in-depth discussions.

A Theory

While we haven’t found the root cause of this, here is a theory:

The browser deletes some entries from the app-shell cache. E.g. it removes the ToC module/component chunk but retains the index.html and main.js/runtime.js that reference it.

Browsers are free to decide when/what to evict from the cache, so different browsers can have different behavior.

Possible Solution

One possible solution is be to:

  1. Detect this situation in the SW (i.e. when a hashed, eagerly cached asset cannot be found in the cache and also does not exist on the server).
  2. Purge the corresponding cache.
  3. Communicate from the SW to the page that the SW and the app are in an unrecoverable state and the page needs to be reloaded.
  4. Have the page listen for the message and handle it accordingly. For example it could show a notification to the user prompting them to reload) or automatically reload the page. (For angular.io, the latter would make more sense, since there is basically no app state to be lost.)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
IgorMinarcommented, Apr 9, 2020

thanks @sonukapoor for taking this on!

2reactions
sonukapoorcommented, Apr 9, 2020

@gkalpak I will try to take a look at this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service Worker Fetching Cached Assets Failure
In the install event of your service worker, you add to the Cache Storage API only one item /src/js/app.js . And when you...
Read more >
Removing buggy service workers - Chrome Developers
Another possibility is that page markup is aggressively cached, and a service worker only returns stale markup responses from a Cache ...
Read more >
Chapter 4. Service Worker Lifecycle and Cache Management
In Chapter 3 we created a dependency between our assets caching successfully and the successful installation of our service worker. By telling the...
Read more >
Handling Service Worker updates – how to keep the app ...
Not applying the Service Worker update might mean that our outdated Service Worker runs for ages and serves our users with assets from...
Read more >
Selective warm up and wind down strategies in a content ...
2009-0254661 titled “Handling Long-Tail Content In A Content Delivery Network (CDN) ... Caching services, rendezvous services, object distribution services, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found