-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workbox): make plugin fully asynchronous
- Loading branch information
pooya parsa
committed
Mar 17, 2019
1 parent
89c1a1d
commit 1eb1190
Showing
2 changed files
with
24 additions
and
24 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,18 @@ | ||
function onError(error) {<% if (options.dev) { %>console.error('Error registering workbox:', error) <% } %>} | ||
|
||
export default function (ctx, inject) { | ||
let workbox = {} | ||
async function register() { | ||
if (!'serviceWorker' in navigator) { | ||
throw new Error('serviceWorker is not supported in current browser!') | ||
} | ||
|
||
try { | ||
// workbox-window does not detects unsupported browsers | ||
if (!'serviceWorker' in navigator) { | ||
throw new Error('serviceWorker is not supported in current browser!') | ||
} | ||
const { Workbox } = await import('workbox-cdn/workbox/workbox-window.<%= options.dev ? 'dev' : 'prod' %>.es5.mjs') | ||
|
||
// 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') | ||
const workbox = new Workbox('<%= options.swURL %>', { | ||
scope: '<%= options.swScope %>' | ||
}) | ||
|
||
workbox = new Workbox('<%= options.swURL %>', { | ||
scope: '<%= options.swScope %>' | ||
}) | ||
await workbox.register() | ||
|
||
workbox.register().catch(onError) | ||
} catch (error) { | ||
onError(error) | ||
} | ||
|
||
// Inject as $workbox | ||
inject('workbox', workbox) | ||
return workbox | ||
} | ||
|
||
window.$workbox = register() | ||
.catch(error => {<% if (options.dev) { %> console.error('Error registering workbox:', error) <% } %>}) |