Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Add SW push/sync feature in dev mode #134

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,20 @@ class DevServer {
this.server.use(function (req, res, next) {
if (req.url === '/service-worker.js') {
res.setHeader('Content-Type', 'text/javascript')
res.send(resetScript)
if (cfg.pwa.swPlugins && cfg.pwa.swPlugins.push) {
res.write(fs.readFileSync(
path.join(appPaths.pwaDir, cfg.pwa.swPlugins.push),
'utf-8'
))
}
if (cfg.pwa.swPlugins && cfg.pwa.swPlugins.sync) {
res.write(fs.readFileSync(
path.join(appPaths.pwaDir, cfg.pwa.swPlugins.sync),
'utf-8'
))
}
res.write(resetScript)
res.end()
}
else {
next()
Expand Down
4 changes: 4 additions & 0 deletions lib/quasar-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ class QuasarConfig {
cfg.pwa = merge({
workboxPluginMode: 'GenerateSW',
workboxOptions: {},
swPlugins: {
push: 'push-service-worker.js',
sync: 'sync-service-worker.js'
},
manifest: {
name: this.pkg.productName || this.pkg.name || 'Quasar App',
short_name: this.pkg.name || 'quasar-pwa',
Expand Down
29 changes: 29 additions & 0 deletions templates/pwa/push-service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file (which will be part of your service worker)
* is picked up by the build system and appended at the end of the service-worker
* To do so, You must active this option in
* quasar.conf > pwa > swPlugins > push by setting the name of this file
* THIS PART OF THE CODE is OUT of Hot Reload managment : You should refresh page to reload it.
*/

/**
* Start listening for web notification (client part), and will display them to the user.
* Fill free to add/remove anypart of the code
*/
self.addEventListener('push', function (event) {
if (!event.data) {
console.error('This push event has no data.', event)
return
}

let msg
try {
msg = event.data.json()
} catch (e) {
console.error('This push event data is not valid JSON.', event.data)
return
}
// Sending message to the service-worker for display
const promiseChain = self.registration.showNotification(msg.title, msg.options)
event.waitUntil(promiseChain)
})
11 changes: 11 additions & 0 deletions templates/pwa/sync-service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* This file (which will be part of your service worker)
* is picked up by the build system and appended at the end of the service-worker
* To do so, You must active this option in
* quasar.conf > pwa > swPlugins > sync by setting the name of this file
* THIS PART OF THE CODE is OUT of Hot Reload managment : You should refresh page to reload it.
*/

self.addEventListener('sync', function (event) {
// Implement your 'sync' event managment.
})