pwa

使你的 VuePress 站点成为一个 渐进式 Web 应用 (PWA).

该插件使用 workbox-build 来生成 Service Worker 文件,并通过 register-service-worker 来注册 Service Worker 。

使用方法

  1. npm i -D @vuepress/plugin-pwa@next
  1. const { pwaPlugin } = require('@vuepress/plugin-pwa')
  2. module.exports = {
  3. plugins: [
  4. pwaPlugin({
  5. // 配置项
  6. }),
  7. ],
  8. }

Web App Manifests

为了使你的网站符合 PWA 的要求,你需要创建一个 Web app manifests 文件,并且为你的 PWA 设置图标、颜色等信息。

你需要将你的 Manifest 文件和图标放置在 Public 目录 下。在下述的示例中,我们假设你正在使用默认的 Public 目录 .vuepress/public

  1. 创建 Manifest 文件

通常是 .vuepress/public/manifest.webmanifest

  1. {
  2. "name": "VuePress",
  3. "short_name": "VuePress",
  4. "description": "Vue-powered Static Site Generator",
  5. "start_url": "/index.html",
  6. "display": "standalone",
  7. "background_color": "#fff",
  8. "theme_color": "#3eaf7c",
  9. "icons": [
  10. {
  11. "src": "/images/icons/android-chrome-192x192.png",
  12. "sizes": "192x192",
  13. "type": "image/png"
  14. },
  15. {
  16. "src": "/images/icons/android-chrome-384x384.png",
  17. "sizes": "384x384",
  18. "type": "image/png"
  19. }
  20. ]
  21. }
  1. 生成 PWA 图标

为了提高你的 PWA 的可用性,你需要生成一些图标,并将它们放置在 Public 目录下。

确保图标的路径匹配 Manifest 文件中的 icons 字段:

  • .vuepress/public/images/icons/android-chrome-192x192.png
  • .vuepress/public/images/icons/android-chrome-384x384.png

::: tip 一些工具可以帮助你做这些事。比如 Favicon Generator 可以帮助你生成图片以及一个 Manifest 文件样例。 :::

  1. 设置 Head 中的标签

你还需要通过 head 配置项来设置一些标签,用来 部署你的 Manifest 文件

  1. module.exports = {
  2. head: [
  3. ['link', { rel: 'manifest', href: '/manifest.webmanifest' }],
  4. ['meta', { name: 'theme-color', content: '#3eaf7c' }],
  5. // ...其他标签
  6. ]
  7. }

配置项

该插件的配置项可以接收 workbox-build 中 generateSW 方法 除了 globDirectoryswDest 以外的所有参数。

比如,你可以设置 skipWaiting: true ,这将在新的 Service Worker 就绪之后立即激活它:

  1. module.exports = {
  2. plugins: [
  3. pwaPlugin({
  4. skipWaiting: true,
  5. }),
  6. ],
  7. }

但是如果你不设置 skipWaiting 或设置为 false ,你就需要手动激活新的 Service Worker 。

  • 对于用户,你可以配合我们提供的 pwa-popup 插件一起使用。
  • 对于开发者,你可以使用该插件提供的 Composition API 来控制 Service Worker 的行为。

serviceWorkerFilename

  • 类型: string

  • 默认值: 'service-worker.js'

  • 详情:

    生成的 Service Worker 文件路径,该路径是 dest 目录的相对路径。

    Service Worker 文件只会在 build 模式下生成。

Composition API

usePwaEvent

  • 详情:

    返回该插件的 Event Emitter 。

    你可以为 register-service-worker 提供的事件添加事件监听器。

  • 示例:

  1. import { usePwaEvent } from '@vuepress/plugin-pwa/lib/client'
  2. export default {
  3. setup() {
  4. const event = usePwaEvent()
  5. event.on('ready', (registration) => {
  6. console.log('Service worker 已经生效。')
  7. })
  8. },
  9. }

useSkipWaiting

  • 参数:
参数 类型 描述
registration ServiceWorkerRegistration 你想要激活的 Service Worker 的 Registration
  • 详情:

    调用 skipWaiting() 来激活处于 Waiting 状态的 Service Worker 。

  • 示例:

  1. import {
  2. usePwaEvent,
  3. useSkipWaiting,
  4. } from '@vuepress/plugin-pwa/lib/client'
  5. export default {
  6. setup() {
  7. const event = usePwaEvent()
  8. event.on('updated', (registration) => {
  9. console.log('在 Waiting 状态的 Service Worker 已经就绪。')
  10. // 激活 Waiting 状态的 Service Worker
  11. useSkipWaiting(registration)
  12. })
  13. },
  14. }