Reputation: 468
I'm trying to replace the default React app serviceworker with a custom one because its setting the index route to "index.html" whereas I need to it go to "200.html" for react-snap.
I'm following this guide EXACTLY https://github.com/facebook/create-react-app/issues/5673#issuecomment-438654051
But get the error:
UnhandledPromiseRejectionWarning: Error: Unable to find a place to inject the manifest. Please ensure that your service worker file contains the following: self.__WB_MANIFEST
at Object.injectManifest
My sw.js looks like this -
if ("function" === typeof importScripts) {
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/3.5.0/workbox-sw.js"
);
/* global workbox */
if (workbox) {
console.log("Workbox is loaded");
/* injection point for manifest files. */
workbox.precaching.precacheAndRoute([]);
/* custom cache rules*/
workbox.routing.registerNavigationRoute("/200.html", {
blacklist: [/^\/_/, /\/[^\/]+\.[^\/]+$/],
});
workbox.routing.registerRoute(
/\.(?:png|gif|jpg|jpeg)$/,
workbox.strategies.cacheFirst({
cacheName: "images",
plugins: [
new workbox.expiration.Plugin({
maxEntries: 60,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
})
);
} else {
console.log("Workbox could not be loaded. No Offline support");
}
}
Everything I've googled and looked on other stackoverflows shows you need to include
workbox.precaching.precacheAndRoute([]);
Which I am. So I don't know why its not working?
My react-scripts to run the workbox:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts --max_old_space_size=4096 build && npm run build-sw",
"build-sw": "node ./src/sw-build.js",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postbuild": "react-snap"
},
Upvotes: 0
Views: 1006
Reputation: 468
The problem was that the version of workbox-build was 5.0 whereas the code/script was using 3.5.0.
Downgrading to 3.5.0 in my package.json fixed the issue.
Upvotes: 1