@vuepress/plugin-pwa

PWA 插件

安装

  1. yarn add -D @vuepress/plugin-pwa@next
  2. # OR npm install -D @vuepress/plugin-pwa@next

使用

  1. module.exports = {
  2. plugins: ['@vuepress/pwa']
  3. }

选项

serviceWorker

  • 类型: boolean
  • 默认值: true如果设置为 true,VuePress 将自动生成并注册一个 Service Worker,用于缓存页面的内容以供离线使用(仅会在生产环境中启用)。

有一个别名化的模块 @sw-event 模块将会 emit 以下事件:

  • sw-ready
  • sw-cached
  • sw-updated
  • sw-offline
  • sw-error

PWA NOTES

serviceWorker 选项仅仅用来控制 service worker,为了让你的网站完全地兼容 PWA,你需要在 .vuepress/public 提供 Manifest 和 icons,更多细节,请参见 MDN docs about the Web App Manifest.此外,只有您能够使用 SSL 部署您的站点时才能启用此功能,因为 service worker 只能在 HTTPs 的 URL 下注册。

generateSWConfig

updatePopup

  • 类型: boolean|popupConfig
  • 默认值: undefined类型 popupConfig 的定义如下:
  1. interface normalPopupConfig {
  2. message: string; // defaults to 'New content is available.'
  3. buttonText: string; // defaults to 'Refresh'
  4. }
  5. interface localedPopupConfig {
  6. [localePath: string]: normalPopupConfig
  7. }
  8. type popupConfig = normalPopupConfig | localedPopupConfig

本选项开启了一个用于刷新内容的弹窗。这个弹窗将会在站点有内容更新时显示出来,并提供了一个 refresh 按钮,允许用户立即刷新内容。

如果没有“刷新”按钮,则只有在所有的 Clients 被关闭后,新的 Service Worker 才会处于活动状态。这意味着用户在关闭你网站的所有标签之前无法看到新内容。但是 refresh 按钮会立即激活新的 Service Worker。

popupComponent

  • 类型: string
  • 默认值: undefined用于替换默认弹出组件的自定义组件。

参考:

从 0.x 迁移

Service Worker

  1. module.exports = {
  2. - serviceWorker: true,
  3. + plugins: ['@vuepress/pwa']
  4. }

SW-Update Popup

  1. module.exports = {
  2. themeConfig: {
  3. - serviceWorker: {
  4. - updatePopup: {
  5. - message: "New content is available.",
  6. - buttonText: "Refresh"
  7. - }
  8. - }
  9. },
  10. + plugins: {
  11. + '@vuepress/pwa': {
  12. + serviceWorker: true,
  13. + updatePopup: {
  14. + message: "New content is available.",
  15. + buttonText: "Refresh"
  16. + }
  17. + }
  18. + }
  19. }

如果你在 i18n 模式下:

  1. module.exports = {
  2. themeConfig: {
  3. '/': {
  4. - serviceWorker: {
  5. - updatePopup: {
  6. - message: "New content is available.",
  7. - buttonText: "Refresh"
  8. - }
  9. - }
  10. },
  11. '/zh/': {
  12. - serviceWorker: {
  13. - updatePopup: {
  14. - message: "发现新内容可用",
  15. - buttonText: "刷新"
  16. - }
  17. - }
  18. }
  19. },
  20. + plugins: {
  21. + '@vuepress/pwa': {
  22. + serviceWorker: true,
  23. + updatePopup: {
  24. + '/': {
  25. + message: "New content is available.",
  26. + buttonText: "Refresh"
  27. + },
  28. + '/zh/': {
  29. + message: "发现新内容可用",
  30. + buttonText: "刷新"
  31. + }
  32. + }
  33. + }
  34. + }

值得一提的是本插件已经内置了上述的 i18n 配置,所以如果你想直接使用默认的 i18n,你可以将上面的配置缩写为:

  1. module.exports = {
  2. plugins: {
  3. '@vuepress/pwa': {
  4. serviceWorker: true,
  5. updatePopup: true
  6. }
  7. }
  8. }

欢迎提交 PR 以增加默认的 i18n 配置.

自定义 SW-Update Popup 的 UI

默认的 SW-Update Popup 组件提供了一个默认插槽,使您能够完全控制弹窗的外观。

首先,您需要在 .vuepress/components 中创建一个全局组件(例如MySWUpdatePopup)。 一个基于默认组件创建的简单组件如下:

  1. <template>
  2. <SWUpdatePopup>
  3. <div
  4. slot-scope="{ enabled, reload, message, buttonText }"
  5. class="my-sw-update-popup">
  6. {{ message }}<br>
  7. <button @click="reload">{{ buttonText }}</button>
  8. </div>
  9. </SWUpdatePopup>
  10. </template>
  11. <script>
  12. import SWUpdatePopup from '@vuepress/plugin-pwa/lib/SWUpdatePopup.vue'
  13. export default {
  14. components: { SWUpdatePopup }
  15. }
  16. </script>
  17. <style>
  18. .my-sw-update-popup {
  19. text-align: right;
  20. position: fixed;
  21. bottom: 20px;
  22. right: 20px;
  23. background-color: #fff;
  24. font-size: 20px;
  25. padding: 10px;
  26. border: 5px solid #3eaf7c;
  27. }
  28. .my-sw-update-popup button {
  29. border: 1px solid #fefefe;
  30. }
  31. </style>

接着,更新你的插件配置:

  1. module.exports = {
  2. plugins: {
  3. '@vuepress/pwa': {
  4. serviceWorker: true,
  5. + popupComponent: 'MySWUpdatePopup',
  6. updatePopup: true
  7. }
  8. }
  9. }

参考: