2

I'm registering my service worker, and in the registered callback, loading some cached files

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('swRoot.js').then(() => {
        console.log('registered');

        System.import('react');
        System.import('react-dom');
        System.import('a').then(({ a }) => console.log('value from a', a));
    }, err => console.log(err));
}

It works, but it seems closing the browser tab, and re-opening it is causing the install event to re-fire, which somehow results in a growing number of fetches / cache hits (see below) to log. Once the page is open, subsequent soft refreshes behave as expected; I only see cache hits for the few files I'm loading.

So the very first time the service worker is installed, I see 3 cache hits, for the 3 files I'm loading. If I close and re-open, I get 6, then 9, etc. In any case, simply refreshing the page shows the original 3 cache hits as expected.

Why does closing and then re-opening the browser tab result in an increasing number of fetch events to fire for the same few files, but not on subsequent refreshes?

The entirety of swRoot.js is below

self.addEventListener('install', function(event) {
    console.log('INSTALLED');

    console.log('ADDING CACHE FILES');
    event.waitUntil(
        caches.open('v1').then(function(cache) {
            return cache.addAll([
                '/react-redux/node_modules/react/dist/react-with-addons.js',
                '/react-redux/node_modules/react-dom/dist/react-dom.js',
                '/react-redux/a.js'
            ]).then(function(){ console.log('cache filling success'); }).catch(function(){ console.log('cache filling failure') });
        })
    );
});

console.log('ADDING FETCH at root level');
self.addEventListener('fetch', function(event) {
    event.respondWith(
        caches.match(event.request)
            .then(function(response) {
                // Cache hit - return response
                if (response) {
                    console.log('cache hit', event.request);
                    return response;
                }
                return fetch(event.request);
            })
    );
});


self.addEventListener('activate', function(event) {
    console.log('ACTIVATE');
});
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • 1
    I wonder if your use of a relative URLs for the path to the SW script is leading to multiple SW registrations, which in turns leads to extra requests. Can you try with an absolute URL, e.g. `'/path/to/swRoot.js'` instead of `'swRoot.js'`? See also: http://stackoverflow.com/a/33881341/385997 – Jeff Posnick Dec 14 '16 at 20:36

2 Answers2

3

So the very first time the service worker is installed, I see 3 cache hits, for the 3 files I'm loading. If I close and re-open, I get 6, then 9, etc. In any case, simply refreshing the page shows the original 3 cache hits as expected.

Most probably is that you are seeing previous logs. To verify, you can show the timeline of every log.

  1. Go to dev tools settings F1 inside console or click on the icon (img1)
  2. Check the option Show timestamps (img2).
  3. Or simple add a console.log(new Date()); inside your fetch event handler.

Img1

dev tools settings


Img2

show timestamps



Now you will be able to identify logs that are from previous requests. Your sw is not making extra request, just the console is keeping old logs.

showing time


To fix it, do right click inside the console and click on clear console history.

Hope this help.

enter image description here

Hosar
  • 5,163
  • 3
  • 26
  • 39
1

It should not be there because the install event is called only once.I think the issue is in your chrome browser force update service worker is checked.Open developer console on chrome -> go to application tab -> service worker and uncheck update on reload.

YoYo Honey Singh
  • 464
  • 1
  • 4
  • 21
  • Thanks for the response - sadly, that box is already unchecked. Contrary to what I wrote originally, it looks like this happens only after closing my browser tab and re-opening it. Refreshing within the page causes everything to behave as normal. – Adam Rackis Dec 12 '16 at 14:21
  • Can you check in your browser's console tab, is **preserve log** checkbox checked.I think it could be the issue. – YoYo Honey Singh Dec 15 '16 at 05:00
  • Yep - that was it, as the other answer said. Thanks again for the response! – Adam Rackis Dec 15 '16 at 06:15