Skip to content
Permalink
Browse files
feat: improve sw.register error handling
  • Loading branch information
pooya parsa committed Mar 17, 2019
1 parent cd6c9cc commit 9aa76f89bdf34cefc018ae602a2514bf9ea38944
Showing 1 changed file with 10 additions and 5 deletions.
@@ -1,20 +1,25 @@
export default async function (ctx, inject) {
function onError(error) {<% if (options.dev) { %>console.error('Error registering workbox:', error) <% } %>}

export default function (ctx, inject) {
let workbox = {}

try {
// workbox-window does not detects unsupported browsers
if (!'serviceWorker' in navigator) {
throw new Error('Serviceworker is not supported!')
throw new Error('serviceWorker is not supported in current browser!')
}

// Use require() instead of import() to prevent creating extra chunk
// Use es5 version to prevent crashing older browsers while parsing bundle
const { Workbox } = require('workbox-cdn/workbox/workbox-window.<%= options.dev ? 'dev' : 'prod' %>.es5.mjs')
workbox = new Workbox('<%= options.swURL %>', {
scope: '<%= options.swScope %>'
})
workbox.register()
} catch (e) {
console.warn('Cannot register workbox:', e)
workbox.register().catch(onError)
} catch (error) {
onError(error)
}
// Inject as $workbox

0 comments on commit 9aa76f8

Please sign in to comment.