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.

`runtimeCaching` doesn't use the `directoryIndex` option

See original GitHub issue

Library Affected: workbox-runtime-caching

Browser & Platform: Google Chrome 72.0

Issue or Feature Request Description: runtimeCaching doesn’t seem to use the directoryIndex option. I want to serve and cache /index.html with runtimeCaching networkFirst approach, for all the routes ending in /.

Using urlPattern: 'index.html' doesn’t seem to work at all, while using urlPattern: /\/$/, makes every different pathname ending in / to be cached separately.

generateSW config

{
    skipWaiting: true,
    clientsClaim: true,
    exclude: [
        /\.map$/,
        /^manifest.*\.js$/,
        'index.html'
    ],
    runtimeCaching: [
    {
        urlPattern: '/\/$/',
        // Apply a network-first strategy.
        handler: 'networkFirst',
    }]
}

service-worker.js

/**
 * Welcome to your Workbox-powered service worker!
 *
 * You'll need to register this file in your web app and you should
 * disable HTTP caching for this file too.
 * See https://goo.gl/nhQhGp
 *
 * The rest of the code is auto-generated. Please don't update this file
 * directly; instead, make changes to your Workbox build configuration
 * and re-run your build process.
 * See https://goo.gl/2aRDsh
 */

importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.6.3/workbox-sw.js");

importScripts(
  "/precache-manifest.32b375c53995d3f7716830693a179759.js"
);

workbox.core.setCacheNameDetails({prefix: "web"});

workbox.skipWaiting();
workbox.clientsClaim();

/**
 * The workboxSW.precacheAndRoute() method efficiently caches and responds to
 * requests for URLs in the manifest.
 * See https://goo.gl/S9QRab
 */
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

workbox.routing.registerRoute("index.html", workbox.strategies.networkFirst(), 'GET');

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
franciscolourencocommented, Apr 14, 2019

@guins @jeffposnick how do you import workbox in the GenerateSW configuration file?

2reactions
jeffposnickcommented, Mar 19, 2019

runtimeCaching doesn’t seem to use the directoryIndex option.

Yup, directoryIndex only applies to precaching.

I want to serve and cache /index.html with runtimeCaching networkFirst approach, for all the routes ending in /.

You’re going to need to write a custom handler to do that. Based on what you describe, the following would work:

runtimeCaching: [{
    urlPattern: new RegExp('/$'),
    handler: ({event}) => {
      const strategy = new workbox.strategies.NetworkFirst();
      return strategy.makeRequest({
        request: '/index.html',
        event,
      });
    },
  }],
Read more comments on GitHub >

github_iconTop Results From Across the Web

workbox-build - Chrome Developers
This method creates a list of URLs to precache, referred to as a "precache manifest", based on the options you provide. It also...
Read more >
Using sw-precache configured with runtimeCaching is not ...
I have installed grunt-sw-precache. The issue is that the service-worker.js file which is created does not include any runtime caching code at ...
Read more >
mod_dir - Apache HTTP Server Version 2.4
The DirectoryIndex directive sets the name of this file. ... If none of the resources exist and the Indexes option is set, the...
Read more >
Workbox Module - Nuxt PWA
(Object) Options to be passed to workbox before using it's modules. ... (String/Regex) Pattern to match assets for runtime caching.
Read more >
gatsby-plugin-offline-next - npm package - Snyk
Ensure you're using the healthiest npm packages ... The runtimeCaching option is no longer supported in 5.x. If you previously used custom ...
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