Search code examples
reactjsservice-workerworkboxworkbox-webpack-plugin

Why am i getting an error saying CacheFirst is not defined?


I'm using workbox to build a service-worker for a React project, I'm not sure what is wrong but I keep getting an error in the console saying Cannot read property 'CacheFirst' of undefined.

The route that I'm fetching is from a nodejs backend-end with an sqlite database the data it returns is from the date listed in the database. Im not sure if that has anything to do with it or its just a Workbox problem. After every update to this file, i did workbox injectManifest and then using serve -s -p3000 build to serve the application in the browser.

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

if (workbox) {
  console.log(`Yay! Workbox is loaded 🎉`);
} else {
  console.log(`Boo! Workbox didn't load 😬`);
}

let d = new Date();

let year = d.getFullYear();
let month = d.getMonth() + 1;
let day = d.getDate();

if (month < 10) {
  month = "0" + month;
}
if (day < 10) {
  day = "0" + day;
}

let today = `${year}-${month}-${day}`;

workbox.routing.registerRoute(
  new RegExp(`http://localhost:5000/routes/${today}`),
  new workbox.stategies.CacheFirst()
);


workbox.routing.registerRoute(
  /^https:\/\/fonts\.googleapis\.com/,
  new workbox.strategies.StaleWhileRevalidate({
    cacheName: "google-fonts-stylesheets"
  })
);


workbox.routing.registerRoute(
  /^https:\/\/fonts\.gstatic\.com/,
  new workbox.strategies.CacheFirst({
    cacheName: "google-fonts-webfonts",
    plugins: [
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      }),
      new workbox.expiration.Plugin({
        maxAgeSeconds: 60 * 60 * 24 * 365,
        maxEntries: 30
      })
    ]
  })
);

workbox.precaching.precacheAndRoute([]);

Solution

  • stategies is misspelled, it should be strategies.