|
1 | 1 | # pwa
|
2 | 2 |
|
3 |
| -> TODO |
| 3 | +> @vuepress/plugin-pwa |
| 4 | +
|
| 5 | +Make your VuePress site a [Progressive Web Application (PWA)](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps). |
| 6 | + |
| 7 | +This plugin uses [workbox-build](https://developers.google.com/web/tools/workbox/modules/workbox-build) to generate service worker file, and uses [register-service-worker](https://github.com/yyx990803/register-service-worker) to register service worker. |
| 8 | + |
| 9 | +## Web App Manifests |
| 10 | + |
| 11 | +To make your website fully compliant with PWA, you need to create a [Web app manifests](https://developer.mozilla.org/en-US/docs/Web/Manifest) file and set the icons, colors, etc. for your PWA. |
| 12 | + |
| 13 | +You need to put your manifest file and icons into the [public files directory](../../guide/assets.md#public-files). In the following example, we assume that you are using the default public directory `.vuepress/public`. |
| 14 | + |
| 15 | +1. Create manifest file |
| 16 | + |
| 17 | +Typically `.vuepress/public/manifest.webmanifest`: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "name": "VuePress", |
| 22 | + "short_name": "VuePress", |
| 23 | + "description": "Vue-powered Static Site Generator", |
| 24 | + "start_url": "/index.html", |
| 25 | + "display": "standalone", |
| 26 | + "background_color": "#fff", |
| 27 | + "theme_color": "#3eaf7c", |
| 28 | + "icons": [ |
| 29 | + { |
| 30 | + "src": "/images/icons/android-chrome-192x192.png", |
| 31 | + "sizes": "192x192", |
| 32 | + "type": "image/png" |
| 33 | + }, |
| 34 | + { |
| 35 | + "src": "/images/icons/android-chrome-384x384.png", |
| 36 | + "sizes": "384x384", |
| 37 | + "type": "image/png" |
| 38 | + } |
| 39 | + ] |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +2. Generate PWA icons |
| 44 | + |
| 45 | +To make your PWA more accessible, you need to generate some icons, and put them inside the public directory. |
| 46 | + |
| 47 | +Make sure the path of icons matches the `icons` field in your manifest file: |
| 48 | + |
| 49 | +- `.vuepress/public/images/icons/android-chrome-192x192.png` |
| 50 | +- `.vuepress/public/images/icons/android-chrome-384x384.png` |
| 51 | + |
| 52 | +::: tip |
| 53 | +Some tools can help to do that. For example, [Favicon Generator](https://realfavicongenerator.net/) would help you to generate icons together with a sample manifest file. |
| 54 | +::: |
| 55 | + |
| 56 | +3. Set tags in head |
| 57 | + |
| 58 | +You also need to set some tags via [head](../config.md#head) option to [deploy the manifest](https://developer.mozilla.org/en-US/docs/Web/Manifest#deploying_a_manifest_with_the_link_tag): |
| 59 | + |
| 60 | +```js |
| 61 | +module.exports = { |
| 62 | + head: [ |
| 63 | + ['link', { rel: 'manifest', href: '/manifest.webmanifest' }], |
| 64 | + ['meta', { name: 'theme-color', content: '#3eaf7c' }], |
| 65 | + // ...other tags |
| 66 | + ] |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Options |
| 71 | + |
| 72 | +This plugin accepts all parameters of workbox-build's [generateSW method](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.generateSW) in its options, except `globDirectory` and `swDest`. |
| 73 | + |
| 74 | +For example, you can set `skipWaiting: true` to auto activate the new service worker once it is ready: |
| 75 | + |
| 76 | +```js |
| 77 | +module.exports = { |
| 78 | + plugins: [ |
| 79 | + [ |
| 80 | + '@vuepress/pwa', |
| 81 | + { |
| 82 | + skipWaiting: true, |
| 83 | + }, |
| 84 | + ], |
| 85 | + ], |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +But if you omit `skipWaiting` or set it to `false`, you have to activate the new service worker manually: |
| 90 | + |
| 91 | +- For users, you can use our [pwa-popup](./pwa-popup.md) plugin together. |
| 92 | +- For developers, you can use our [composition API](#composition-api) to take control of the service worker behavior. |
| 93 | + |
| 94 | +### serviceWorkerFilename |
| 95 | + |
| 96 | +- Type: `string` |
| 97 | + |
| 98 | +- Default: `'service-worker.js'` |
| 99 | + |
| 100 | +- Details: |
| 101 | + |
| 102 | + File path of the generated service worker file, which is relative to the [dest](../config.md#dest) directory. |
| 103 | + |
| 104 | + The service worker file will only be generated in `build` mode. |
| 105 | + |
| 106 | +## Composition API |
| 107 | + |
| 108 | +### usePwaEvent |
| 109 | + |
| 110 | +- Details: |
| 111 | + |
| 112 | + Returns the event emitter of this plugin. |
| 113 | + |
| 114 | + You can add listener function to events that provided by [register-service-worker](https://github.com/yyx990803/register-service-worker). |
| 115 | + |
| 116 | +- Example: |
| 117 | + |
| 118 | +```ts |
| 119 | +import { usePwaEvent } from '@vuepress/plugin-pwa/lib/composables' |
| 120 | + |
| 121 | +export default { |
| 122 | + setup() { |
| 123 | + event = usePwaEvent() |
| 124 | + event.on('ready', (registration) => { |
| 125 | + console.log('Service worker is active.') |
| 126 | + }) |
| 127 | + }, |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +### useSkipWaiting |
| 132 | + |
| 133 | +- Parameters: |
| 134 | + |
| 135 | +| Parameter | Type | Description | |
| 136 | +| ------------ | --------------------------- | -------------------------------------------------------- | |
| 137 | +| registration | `ServiceWorkerRegistration` | The registration of the service worker you want activate | |
| 138 | + |
| 139 | +- Details: |
| 140 | + |
| 141 | + Call [skipWaiting()](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting) to activate the waiting service worker. |
| 142 | + |
| 143 | +- Example: |
| 144 | + |
| 145 | +```ts |
| 146 | +import { |
| 147 | + usePwaEvent, |
| 148 | + useSkipWaiting, |
| 149 | +} from '@vuepress/plugin-pwa/lib/composables' |
| 150 | + |
| 151 | +export default { |
| 152 | + setup() { |
| 153 | + event = usePwaEvent() |
| 154 | + event.on('updated', (registration) => { |
| 155 | + console.log('The waiting service worker is available.') |
| 156 | + // activate the waiting service worker |
| 157 | + useSkipWaiting(registration) |
| 158 | + }) |
| 159 | + }, |
| 160 | +} |
| 161 | +``` |
0 commit comments