Skip to content

Instantly share code, notes, and snippets.

@bvjebin
Last active April 20, 2019 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvjebin/9ab15fe196d3c353e8c69c37588f8ea3 to your computer and use it in GitHub Desktop.
Save bvjebin/9ab15fe196d3c353e8c69c37588f8ea3 to your computer and use it in GitHub Desktop.
Medium article - service worker code
import * as routing from "workbox-routing";
import * as core from "workbox-core";
import * as strategies from "workbox-strategies";
import * as expiration from "workbox-cache-expiration";
import * as cacheableResponse from "workbox-cacheable-response";
self.addEventListener("sync", function (event) {
console.log("Event from sw: ", event);
});
self.addEventListener("install", function () {
console.log("Installed worker");
});
self.addEventListener("activate", function () {
console.log("Simply Activated worker");
if (self.clients && self.clients.claim) {
self.clients.claim();
}
});
core.setCacheNameDetails({
prefix: "nightwalker",
suffix: "v1",
precache: "precache",
runtime: "runtime",
googleAnalytics: "ga"
});
routing.registerRoute(
/(_next\/static\/(.*).js$)|(_next\/static\/(.*).css$)/,
new strategies.NetworkFirst({
networkTimeoutSeconds: 15,
plugins: [
new expiration.Plugin({
maxEntries: 30,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
new cacheableResponse.Plugin({
statuses: [0, 200],
})
],
})
);
routing.registerRoute(
/(\/static\/images)|\.(?:png|gif|jpg|jpeg|svg)$/,
new strategies.CacheFirst({
plugins: [
new expiration.Plugin({
maxEntries: 30,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
new cacheableResponse.Plugin({
statuses: [0, 200],
})
],
}),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment