-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($pwa): add themeConfig.serviceWorker.updatePopup option (close: #…
- Loading branch information
1 parent
887a0a6
commit 14dbd1e
Showing
9 changed files
with
187 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export default class SWUpdateEvent { | ||
constructor (registration) { | ||
Object.defineProperty(this, 'registration', { | ||
value: registration, | ||
configurable: true, | ||
writable: true | ||
}) | ||
} | ||
|
||
/** | ||
* Check if the new service worker exists or not. | ||
*/ | ||
update () { | ||
return this.registration.update() | ||
} | ||
|
||
/** | ||
* Activate new service worker to work 'location.reload()' with new data. | ||
*/ | ||
skipWaiting () { | ||
const worker = this.registration.waiting | ||
if (!worker) { | ||
return Promise.resolve() | ||
} | ||
|
||
console.log('[vuepress:sw] Doing worker.skipWaiting().') | ||
return new Promise((resolve, reject) => { | ||
const channel = new MessageChannel() | ||
|
||
channel.port1.onmessage = (event) => { | ||
console.log('[vuepress:sw] Done worker.skipWaiting().') | ||
if (event.data.error) { | ||
reject(event.data.error) | ||
} else { | ||
resolve(event.data) | ||
} | ||
} | ||
|
||
worker.postMessage({ type: 'skip-waiting' }, [channel.port2]) | ||
}) | ||
} | ||
} | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<template> | ||
<transition name="sw-update-popup"> | ||
<div v-if="enabled" class="sw-update-popup"> | ||
{{message}}<br> | ||
<button @click="reload">{{buttonText}}</button> | ||
</div> | ||
</transition> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
updateEvent: { | ||
type: Object, | ||
default: null | ||
} | ||
}, | ||
computed: { | ||
popupConfig () { | ||
for (const config of [this.$themeLocaleConfig, this.$site.themeConfig]) { | ||
const sw = config.serviceWorker | ||
if (sw && sw.updatePopup) { | ||
return typeof sw.updatePopup === 'object' ? sw.updatePopup : {} | ||
} | ||
} | ||
return null | ||
}, | ||
enabled () { | ||
return Boolean(this.popupConfig && this.updateEvent) | ||
}, | ||
message () { | ||
const c = this.popupConfig | ||
return (c && c.message) || 'New content is available.' | ||
}, | ||
buttonText () { | ||
const c = this.popupConfig | ||
return (c && c.buttonText) || 'Refresh' | ||
} | ||
}, | ||
methods: { | ||
reload () { | ||
if (this.updateEvent) { | ||
this.updateEvent.skipWaiting().then(() => { | ||
location.reload(true) | ||
}) | ||
this.updateEvent = null | ||
} | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="stylus"> | ||
@import './styles/config.styl' | ||
.sw-update-popup | ||
position fixed | ||
right 1em | ||
bottom 1em | ||
padding 1em | ||
border 1px solid $accentColor | ||
border-radius 3px | ||
background #fff | ||
box-shadow 0 4px 16px rgba(0, 0, 0, 0.5) | ||
text-align center | ||
button | ||
margin-top 0.5em | ||
padding 0.25em 2em | ||
.sw-update-popup-enter-active, .sw-update-popup-leave-active | ||
transition opacity 0.3s, transform 0.3s | ||
.sw-update-popup-enter, .sw-update-popup-leave-to | ||
opacity 0 | ||
transform translate(0, 50%) scale(0.5) | ||
</style> |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
addEventListener('message', event => { | ||
const replyPort = event.ports[0] | ||
const message = event.data | ||
if (replyPort && message && message.type === 'skip-waiting') { | ||
event.waitUntil( | ||
self.skipWaiting().then( | ||
() => replyPort.postMessage({ error: null }), | ||
error => replyPort.postMessage({ error }) | ||
) | ||
) | ||
} | ||
}) |
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