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.

Precaching fails to cache JS on Firefox, but does so correctly on Chrome

See original GitHub issue

Library Affected: workbox-precaching, workbox-webpack-plugin

Browser & Platform: Firefox v96

Issue or Feature Request Description: We’re having an issue with Workbox. The codebase includes Workbox for PWA and caching support. On first load, Chrome will download the app files, and store them in the cache - this includes JS, CSS, and HTML. All working correctly.

Firefox, however, is only storing the CSS files. This makes it impossible to use the app offline on Firefox. Is this a bug or a problem with the code? I thought it was working before on Firefox, but I can’t confirm that, so did an update (on either Workbox or FF) break it?

The exception for JS on FF, is the Cache.worker.js file, that is cached appropriately. Nothing else though.

The app is built with Vue. But I don’t use the vue-cli-pwa as that was using an older version of Workbox. Instead, the vue.config.js uses workbox-webpack-plugin directly to compile the service worker.

vue.config.js file:

const CompressionPlugin = require('compression-webpack-plugin');
const { InjectManifest } = require('workbox-webpack-plugin');
module.exports = {
  chainWebpack: (config) => {
    config.module
      .rule('worker')
      .test(/\.worker\.js$/)
      .use('worker-loader')
      .loader('worker-loader')
      .end();
    // Exclude worker files from js compilation
    config.module.rule('js').exclude.add(/\.worker\.js$/);
    // Exclude the cache worker from worker compilation as it's compiled below with "InjectManifest"
    config.module.rule('worker').exclude.add(/Cache.worker\.js$/);
  },
  configureWebpack: {
    plugins: [
      // new CompressionPlugin(),
      new InjectManifest({
        maximumFileSizeToCacheInBytes: 4000000,
        swDest: 'Cache.worker.js',
        swSrc: './src/workers/Cache.worker.js'
      })
    ]
  }
};

Cache.worker.js file:

import { precacheAndRoute } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import {
  CacheFirst,
  StaleWhileRevalidate
} from 'workbox-strategies';
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
import { ExpirationPlugin } from 'workbox-expiration';
// When told to, promote the new SW over the old SW
self.addEventListener('message', (event) => {
  if (event.data === 'skipWaiting') {
    self.skipWaiting();
  }
});
// Precache files
precacheAndRoute(self.__WB_MANIFEST);
// Other caching happens below here, but isn't relevant to the precache

registerServiceWorker.js which is fired on App load:

import { register } from 'register-service-worker';
/**
 * Refresh site function
 * Returns a bound function bound to the SW itself
 */
function refresh(registration) {
  return function innerRefresh() {
    if (registration.waiting) registration.waiting.postMessage('skipWaiting');
  };
}
if (process.env.NODE_ENV === 'production') {
  register(`${process.env.BASE_URL}Cache.worker.js`, {
    error(error) {
      console.error('Error during service worker registration:', error);
    },
    updated(registration) {
      window.dispatchEvent(new CustomEvent('refresh', {
        detail: {
          refreshFunction: refresh(registration)
        }
      }));
    }
  });
}

The actual WB_Manifest includes all the JS/CSS/HTML required.

Everything works completely correctly on Chrome. I think it used to as well on Firefox, but not anymore. The Firefox cache is full of CSS files, but nothing else. The Chrome cache has the full suite of JS/HTML/CSS.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffposnickcommented, Jan 14, 2022

All of the resources appear to be cached in both Chrome and Firefox in the example web app you passed along.

That being said, you have 793 entries in your precache manifest, which is a lot. You should start with ensuring that .gz files aren’t added to the precache manifest:

new InjectManifest({
  // ...other config...
  exclude: [/\.gz$/],
});

You can verify what’s in the cache via commands in the JS console, since Firefox’s cache storage viewer apparently won’t show more than the first (50? 100? I didn’t count) entries. All of those entries are CSS-related, so that might be why you thought that was all that was cached.

k = await caches.keys();
c = await caches.open(k[0]);
reqs = await c.keys();
reqs.forEach(r => console.log(r.url))
0reactions
huykoncommented, Dec 2, 2022

In my case concern it only show fail cache on chrome browser. You can check the site https://venia.magento.com/ image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browser Cache breaks the website in Mozilla Firefox, but ...
I am a Web Developer, fixing a website for my client. After the page is cached in Mozilla Firefox the layout breaks, when...
Read more >
Chrome and Firefox JavaScript execution caching behavior
I am working on a web application that involves doing a some computation with JavaScript. In a benchmark test, I found Chrome is...
Read more >
workbox-precaching - Chrome Developers
Easily precache a set of files and efficiently manage updates to files. ... cached response is not present (due to some unexpected error), ......
Read more >
Why Prefetch Is Broken - Jeff Kaufman
Prefetching the image for slide N+1 when viewing slide N made each transition practically instant. This works for images, but also works for...
Read more >
199110 – PWA in iOS use old assets after publish new ...
js are publish, but the PWA in iOS load the old assets present in precache. In Safari it works correctly, but when you're...
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