@vuepress/plugin-pwa

PWA plugin

Install

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

Usage

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

Options

serviceWorker

  • Type: boolean
  • Default: true

If set to true, VuePress will automatically generate and register a service worker that caches the content for offline use (only enabled in production).

There is a aliased module @sw-event module that will also be emitting the following events:

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

PWA NOTES

The serviceWorker option only handles the service worker. To make your site fully PWA-compliant, you will need to provide the Web App Manifest and icons in .vuepress/public. For more details, see MDN docs about the Web App Manifest.

Also, only enable this if you are able to deploy your site with SSL, since service worker can only be registered under HTTPs URLs.

generateSWConfig

  • Type: object
  • Default: {}

generateSW config of workbox-build.

updatePopup

  • Type: boolean|object
  • Default: undefined

The definition of type popupConfig is as follows:

interface normalPopupConfig {
  message: string; // defaults to 'New content is available.'
  buttonText: string; // defaults to 'Refresh'
}

interface localedPopupConfig {
  [localePath: string]: normalPopupConfig
}

type popupConfig = normalPopupConfig | localedPopupConfig

This option enables the popup to refresh contents. The popup will be shown when the site is updated (i.e. service worker is updated). It provides refresh button to allow users to refresh contents immediately.

If without the refresh button, the new service worker will be active after all clients are closed. This means that visitors cannot see new contents until they close all tabs of your site. But the refresh button activates the new service worker immediately.

popupComponent

  • Type: string
  • Default: undefined

A custom component to replace the default popup component.

Also see:

Migration from 0.x

Service Worker

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

SW-Update Popup

module.exports = {
  themeConfig: {
-   serviceWorker: {
-     updatePopup: {
-        message: "New content is available.",
-        buttonText: "Refresh"
-     }
-   }
  },
+  plugins: {
+   '@vuepress/pwa': {
+      serviceWorker: true,
+      updatePopup: {
+        message: "New content is available.",
+        buttonText: "Refresh"
+      }
+    }
+ }
}

For i18n user:

module.exports = {
  themeConfig: {
    '/': {
-     serviceWorker: {
-       updatePopup: {
-         message: "New content is available.",
-         buttonText: "Refresh"
-       }
-     }
    },
    '/zh/': {
-     serviceWorker: {
-       updatePopup: {
-         message: "发现新内容可用",
-         buttonText: "刷新"
-       }
-     }
    }
  },
+  plugins: {
+    '@vuepress/pwa': {
+      serviceWorker: true,
+      updatePopup: {
+        '/': {
+          message: "New content is available.",
+          buttonText: "Refresh"
+        },
+        '/zh/': {
+          message: "发现新内容可用",
+          buttonText: "刷新"
+        }
+      }
+    }
+  }

It's worth mentioning that the PWA plugin has above i18n built in, so if you want to use the default i18n directly, you can abbreviate the above configuration as:

module.exports = {
  plugins: {
    '@vuepress/pwa': {
      serviceWorker: true,
      updatePopup: true
    }
  }
}

Feel free to submit PRs to improve the default i18n configuration.

Customize the UI of SW-Update Popup

The default sw-update popup component provides a default slot which gives you the ability to fully control the appearance of the popup.

First, you need to create a global component (e.g. MySWUpdatePopup) at .vuepress/components. A simple component created based on the default component is as follows:

<template>
  <SWUpdatePopup>
    <div
      slot-scope="{ enabled, reload, message, buttonText }"
      class="my-sw-update-popup">
      {{ message }}<br>
      <button @click="reload">{{ buttonText }}</button>
    </div>
  </SWUpdatePopup>
</template>

<script>
import SWUpdatePopup from '@vuepress/plugin-pwa/lib/SWUpdatePopup.vue'

export default {
  components: { SWUpdatePopup }
}
</script>

<style>
.my-sw-update-popup {
  text-align: right;
  position: fixed;
  bottom: 20px;
  right: 20px;
  background-color: #fff;
  font-size: 20px;
  padding: 10px;
  border: 5px solid #3eaf7c;
}

.my-sw-update-popup button {
  border: 1px solid #fefefe;
}
</style>

Then, update your plugin config:

module.exports = {
   plugins: {
    '@vuepress/pwa': {
       serviceWorker: true,
+      popupComponent: 'MySWUpdatePopup',
       updatePopup: true
     }
  }
}

Also see: