Created
November 25, 2019 13:38
CRA Workbox change
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
const fs = require("fs"); | |
function template (preCacheFile, blackList) { | |
return `/** | |
* 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 code below was initially generated by WorkBox but because | |
* CRA doesn't allow blacklist customization "yet", run the node | |
* command "bin/changeServiceWorker.js" after each build. | |
*/ | |
importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js"); | |
importScripts( | |
"/${preCacheFile}" | |
); | |
self.addEventListener('message', (event) => { | |
if (event.data && event.data.type === 'SKIP_WAITING') { | |
self.skipWaiting(); | |
} | |
}); | |
workbox.core.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.precacheAndRoute(self.__precacheManifest, {}); | |
// teste | |
workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("/index.html"), { | |
blacklist: [/^\\/_/, /\\/[^\\/?]+\\.[^\\/]+$/${blackList ? "," + blackList : ""}], | |
}); | |
`; | |
} | |
const blackList = `/api/`; | |
const buildDir = "./build"; | |
const dir = fs.readdirSync(buildDir); | |
const swFile = dir.filter(function (elm) { | |
return elm.match(/precache-manifest/gi); | |
})[0]; | |
if (!swFile) { | |
throw new Error("Not found file precache-manifest"); | |
} | |
fs.writeFileSync(buildDir + "/service-worker.js", template(swFile, blackList)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add in your package.json