Last active
April 20, 2019 08:38
-
-
Save bvjebin/9ab15fe196d3c353e8c69c37588f8ea3 to your computer and use it in GitHub Desktop.
Medium article - service worker code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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