I'm trying to set up a service worker using sw-precache to cache assets from CDNJS to be used offline. Unfortunately, this doesn't seem to be working. Here's what I have so far:
Before any of the scripts are loaded in index.html
:
<script type='text/javascript'>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/js/service_worker.js')
.then(function() {
console.log("Service Worker Registered")
})
}
</script>
And here's the Gulp task I'm using to generate the service worker:
const config = require('../config')
const gulp = require('gulp')
const swPrecache = require('sw-precache')
gulp.task('serviceWorker', (callback) => {
swPrecache.write(config.build.serviceWorker, {
runtimeCaching: [
{
urlPattern: /^https:\/\/cdnjs\.cloudflare\.com/,
handler: 'networkFirst'
}
]
}, callback)
})
When I run the app, I see Service Worker Registered
logged to the console. However, if I disconnect my wifi the app fails to loads the scripts. What am I doing wrong?