|
1 | 1 | # @vuepress/plugin-pwa
|
2 | 2 |
|
3 | 3 | > PWA plugin for vuepress
|
4 |
| -
|
5 |
| -## Usage |
6 |
| - |
7 |
| -```javascript |
8 |
| -module.exports = { |
9 |
| - plugins: ['@vuepress/pwa'] |
10 |
| -} |
11 |
| -``` |
12 |
| - |
13 |
| -## Options |
14 |
| - |
15 |
| -### serviceWorker |
16 |
| - |
17 |
| -- Type: `boolean` |
18 |
| -- Default: `true` |
19 |
| - |
20 |
| -If set to `true`, VuePress will automatically generate and register a service worker that caches the content for offline use (only enabled in production). |
21 |
| - |
22 |
| -### updatePopup |
23 |
| - |
24 |
| -- Type: `boolean|popupConfig` |
25 |
| -- Default: `undefined` |
26 |
| - |
27 |
| -```typescript |
28 |
| -interface normalPopupConfig { |
29 |
| - message: string; // defaults to 'New content is available.' |
30 |
| - buttonText: string; // defaults to 'Refresh' |
31 |
| -} |
32 |
| - |
33 |
| -interface localedPopupConfig { |
34 |
| - [localePath: string]: normalPopupConfig |
35 |
| -} |
36 |
| - |
37 |
| -type popupConfig = normalPopupConfig | localedPopupConfig |
38 |
| -``` |
39 |
| -
|
40 |
| -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. |
41 |
| -
|
42 |
| -> If without the `refresh` button, the new service worker will be active after all [clients](https://developer.mozilla.org/en-US/docs/Web/API/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. |
43 |
| -
|
44 |
| -### popupComponent |
45 |
| -
|
46 |
| -- Type: `string` |
47 |
| -- Default: `undefined` |
48 |
| -
|
49 |
| -A custom component to replace the default popup component. |
50 |
| -
|
51 |
| -**Also see:** |
52 |
| -
|
53 |
| -- [Customize the SW-Update Popup UI](#customize-the-sw-update-popup-ui) |
54 |
| -
|
55 |
| -## Migration from 0.x.x |
56 |
| -
|
57 |
| -Now that we have plugin API, all features' options that are in plugin's areas will become plugin options. |
58 |
| -
|
59 |
| -### Service Worker |
60 |
| -
|
61 |
| -``` diff |
62 |
| -module.exports = { |
63 |
| -- serviceWorker: true, |
64 |
| -+ plugins: ['@vuepress/pwa'] |
65 |
| -} |
66 |
| -``` |
67 |
| - |
68 |
| -### SW-Update Popup |
69 |
| - |
70 |
| -``` diff |
71 |
| -module.exports = { |
72 |
| - themeConfig: { |
73 |
| -- serviceWorker: { |
74 |
| -- updatePopup: { |
75 |
| -- message: "New content is available.", |
76 |
| -- buttonText: "Refresh" |
77 |
| -- } |
78 |
| -- } |
79 |
| - }, |
80 |
| -+ plugins: { |
81 |
| -+ '@vuepress/pwa': { |
82 |
| -+ serviceWorker: true, |
83 |
| -+ updatePopup: { |
84 |
| -+ message: "New content is available.", |
85 |
| -+ buttonText: "Refresh" |
86 |
| -+ } |
87 |
| -+ } |
88 |
| -+ } |
89 |
| -} |
90 |
| -``` |
91 |
| - |
92 |
| -For i18n user: |
93 |
| - |
94 |
| -``` diff |
95 |
| -module.exports = { |
96 |
| - themeConfig: { |
97 |
| - '/': { |
98 |
| -- serviceWorker: { |
99 |
| -- updatePopup: { |
100 |
| -- message: "New content is available.", |
101 |
| -- buttonText: "Refresh" |
102 |
| -- } |
103 |
| -- } |
104 |
| - }, |
105 |
| - '/zh/': { |
106 |
| -- serviceWorker: { |
107 |
| -- updatePopup: { |
108 |
| -- message: "发现新内容可用", |
109 |
| -- buttonText: "刷新" |
110 |
| -- } |
111 |
| -- } |
112 |
| - } |
113 |
| - }, |
114 |
| -+ plugins: { |
115 |
| -+ '@vuepress/pwa': { |
116 |
| -+ serviceWorker: true, |
117 |
| -+ updatePopup: { |
118 |
| -+ '/': { |
119 |
| -+ message: "New content is available.", |
120 |
| -+ buttonText: "Refresh" |
121 |
| -+ }, |
122 |
| -+ '/zh/': { |
123 |
| -+ message: "发现新内容可用", |
124 |
| -+ buttonText: "刷新" |
125 |
| -+ } |
126 |
| -+ } |
127 |
| -+ } |
128 |
| -+ } |
129 |
| -``` |
130 |
| - |
131 |
| -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: |
132 |
| - |
133 |
| -```js |
134 |
| -module.exports = { |
135 |
| - plugins: { |
136 |
| - '@vuepress/pwa': { |
137 |
| - serviceWorker: true, |
138 |
| - updatePopup: true |
139 |
| - } |
140 |
| - } |
141 |
| -} |
142 |
| -``` |
143 |
| - |
144 |
| -Feel free to submit PRs to improve the default [i18n configuration](https://github.com/vuejs/vuepress/blob/next/packages/%40vuepress/plugin-pwa/lib/i18n.js). |
145 |
| - |
146 |
| -## Customize the SW-Update Popup UI |
147 |
| - |
148 |
| -The default sw-update popup component provides a default slot which gives you the ability to fully control the appearence of the popup. |
149 |
| - |
150 |
| -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: |
151 |
| - |
152 |
| -```vue |
153 |
| -<template> |
154 |
| - <SWUpdatePopup> |
155 |
| - <div |
156 |
| - slot-scope="{ enabled, reload, message, buttonText }" |
157 |
| - class="my-sw-update-popup"> |
158 |
| - {{ message }}<br> |
159 |
| - <button @click="reload">{{ buttonText }}</button> |
160 |
| - </div> |
161 |
| - </SWUpdatePopup> |
162 |
| -</template> |
163 |
| -
|
164 |
| -<script> |
165 |
| -import SWUpdatePopup from '@vuepress/plugin-pwa/lib/SWUpdatePopup.vue' |
166 |
| -
|
167 |
| -export default { |
168 |
| - components: { SWUpdatePopup } |
169 |
| -} |
170 |
| -</script> |
171 |
| -
|
172 |
| -<style> |
173 |
| -.my-sw-update-popup { |
174 |
| - text-align: right; |
175 |
| - position: fixed; |
176 |
| - bottom: 20px; |
177 |
| - right: 20px; |
178 |
| - background-color: #fff; |
179 |
| - font-size: 20px; |
180 |
| - padding: 10px; |
181 |
| - border: 5px solid #3eaf7c; |
182 |
| -} |
183 |
| -
|
184 |
| -.my-sw-update-popup button { |
185 |
| - border: 1px solid #fefefe; |
186 |
| -} |
187 |
| -</style> |
188 |
| -``` |
189 |
| - |
190 |
| -Then, update your plugin: |
191 |
| - |
192 |
| -``` diff |
193 |
| -module.exports = { |
194 |
| - plugins: { |
195 |
| - '@vuepress/pwa': { |
196 |
| - serviceWorker: true, |
197 |
| -+ popupComponent: 'MySWUpdatePopup', |
198 |
| - updatePopup: true |
199 |
| - } |
200 |
| - } |
201 |
| -} |
202 |
| -``` |
203 |
| - |
204 |
| -**Also see:** |
205 |
| - |
206 |
| -- [VuePress > Using Components](https://vuepress.vuejs.org/guide/using-vue.html#using-components) |
207 |
| -- [Vue > Scoped Slots](https://vuejs.org/v2/guide/components-slots.html#Scoped-Slots) |
0 commit comments