Skip to content

Commit

Permalink
feat: improve sw.register error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed Mar 17, 2019
1 parent cd6c9cc commit 9aa76f8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/workbox/templates/sw.register.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9aa76f8

Please sign in to comment.