Skip to content

Commit 487d8c4

Browse files
authored
feat(plugin-pwa): improve pwa support (#301)
1 parent 0915677 commit 487d8c4

File tree

8 files changed

+185
-189
lines changed

8 files changed

+185
-189
lines changed

docs/.vuepress/configs/head.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ export const head: HeadConfig[] = [
2121
],
2222
['link', { rel: 'manifest', href: '/manifest.webmanifest' }],
2323
['meta', { name: 'application-name', content: 'VuePress' }],
24-
['meta', { name: 'apple-mobile-web-app-title', content: 'VuePress' }],
25-
['meta', { name: 'apple-mobile-web-app-status-bar-style', content: 'black' }],
2624
[
2725
'link',
2826
{ rel: 'apple-touch-icon', href: `/images/icons/apple-touch-icon.png` },
@@ -35,6 +33,5 @@ export const head: HeadConfig[] = [
3533
color: '#3eaf7c',
3634
},
3735
],
38-
['meta', { name: 'msapplication-TileColor', content: '#3eaf7c' }],
3936
['meta', { name: 'theme-color', content: '#3eaf7c' }],
4037
]

docs/plugins/pwa/pwa/config.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,15 @@ Special settings for better supporting Safari, ignoring these options are safe.
146146
- Type: `string`
147147
- Details: Icon link used by Safari.
148148

149-
#### apple.statusBarColor
150-
151-
- Type: `"black" | "white"`
152-
- Default: `"black"`
153-
- Details: Status bar color for Safari
154-
155149
#### apple.maskIcon
156150

157151
- Type: `string`
158152
- Details: Safari mask icon
159153

160-
### msTile
161-
162-
Special settings for Microsoft tiles, ignoring these options are safe.
163-
164-
#### msTile.image
165-
166-
- Type: `string`
167-
- Details: Tile image
168-
169-
#### msTile.color
154+
#### apple.statusBarColor
170155

171-
- Type: `string`
172-
- Default value: `themeColor`
173-
- Details: Tile color
156+
- Type: `'black-translucent' | 'black' | 'default`
157+
- Details: Status bar color for Safari
174158

175159
### foundComponent
176160

docs/zh/plugins/pwa/pwa/config.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,15 @@
144144
- 类型:`string`
145145
- 详情:填入苹果使用的图标地址,推荐 152×152 大小
146146

147-
#### apple.statusBarColor
148-
149-
- 类型:`"black" | "white"`
150-
- 默认值:`"black"`
151-
- 详情:Safari 状态栏颜色
152-
153147
#### apple.maskIcon
154148

155149
- 类型:`string`
156150
- 详情:Safari 图标
157151

158-
### msTile
159-
160-
针对微软磁贴的特殊设置,忽略它们是安全的。
161-
162-
#### msTile.image
163-
164-
- 类型:`string`
165-
- 详情:磁贴图标
166-
167-
#### msTile.color
152+
#### apple.statusBarColor
168153

169-
- 类型:`string`
170-
- 默认值:`themeColor`
171-
- 详情:磁贴颜色。
154+
- 类型:`'black-translucent' | 'black' | 'default`
155+
- 详情:Safari 状态栏颜色
172156

173157
### foundComponent
174158

plugins/pwa/plugin-pwa/src/node/helper.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export const appendBase = (base: string, options: PwaPluginOptions): void => {
4646
options.apple.maskIcon = appendBaseToLink(base, options.apple.maskIcon)
4747
}
4848

49-
if (options.msTile && options.msTile.image)
50-
options.msTile.image = appendBaseToLink(base, options.msTile.image)
51-
5249
if (options.manifest)
5350
options.manifest = appendBaseToManifest(base, options.manifest)
5451
}

plugins/pwa/plugin-pwa/src/node/injectLinksToHead.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import type { HeadConfig } from 'vuepress/core'
1+
import type { App, HeadConfig } from 'vuepress/core'
22
import { isPlainObject } from 'vuepress/shared'
33
import type { PwaPluginOptions } from './options.js'
44

55
export const injectLinksToHead = (
6-
{
7-
favicon,
8-
manifest,
9-
themeColor = '#46bd87',
10-
apple,
11-
msTile,
12-
}: PwaPluginOptions,
13-
base = '/',
14-
head: HeadConfig[] = [],
6+
app: App,
7+
{ favicon, manifest, themeColor = '#46bd87', apple }: PwaPluginOptions,
158
): HeadConfig[] => {
9+
const { base, head } = app.options
1610
const metaKeys: string[] = []
1711
const linkKeys: string[] = []
1812

@@ -22,7 +16,9 @@ export const injectLinksToHead = (
2216
else if (item[0] === 'link') linkKeys.push(item[1].rel as string)
2317
})
2418

19+
const appTitle = manifest?.name ?? app.siteData.title
2520
let fallBackIcon = ''
21+
2622
const setLink = (
2723
rel: string,
2824
href: string,
@@ -40,8 +36,22 @@ export const injectLinksToHead = (
4036
head.push(['meta', { name, content, ...more }])
4137
}
4238

39+
if (appTitle) setMeta('application-name', appTitle)
40+
41+
setMeta('mobile-web-app-capable', 'yes')
42+
setMeta('theme-color', themeColor || '#46bd87')
43+
44+
// eslint-disable-next-line @typescript-eslint/no-deprecated
45+
if (isPlainObject(apple) && apple.statusBarColor)
46+
// eslint-disable-next-line @typescript-eslint/no-deprecated
47+
setMeta('apple-mobile-web-app-status-bar-style', apple.statusBarColor)
48+
4349
if (favicon) setLink('icon', favicon)
4450

51+
setLink('manifest', `${base}manifest.webmanifest`, {
52+
crossorigin: 'use-credentials',
53+
})
54+
4555
if (manifest?.icons) {
4656
const { icons } = manifest
4757

@@ -56,34 +66,14 @@ export const injectLinksToHead = (
5666
}
5767
}
5868

59-
setLink('manifest', `${base}manifest.webmanifest`, {
60-
crossorigin: 'use-credentials',
61-
})
62-
setMeta('theme-color', themeColor || '#46bd87')
63-
6469
if (isPlainObject(apple) && (apple.icon || fallBackIcon)) {
6570
setLink('apple-touch-icon', apple.icon || fallBackIcon)
66-
setMeta('apple-mobile-web-app-capable', 'yes')
67-
setMeta(
68-
'apple-mobile-web-app-status-bar-style',
69-
apple.statusBarColor || 'black',
70-
)
7171
if (apple.maskIcon)
7272
setLink('mask-icon', apple.maskIcon, {
7373
color: themeColor || '#46bd87',
7474
})
7575
} else if (apple !== false && fallBackIcon) {
7676
setLink('apple-touch-icon', fallBackIcon)
77-
setMeta('apple-mobile-web-app-capable', 'yes')
78-
setMeta('apple-mobile-web-app-status-bar-style', 'black')
79-
}
80-
81-
if (isPlainObject(msTile) && (msTile.image || fallBackIcon)) {
82-
setMeta('msapplication-TileImage', msTile.image || fallBackIcon)
83-
setMeta('msapplication-TileColor', msTile.color || themeColor || '#46bd87')
84-
} else if (msTile !== false && fallBackIcon) {
85-
setMeta('msapplication-TileImage', fallBackIcon)
86-
setMeta('msapplication-TileColor', themeColor || '#46bd87')
8777
}
8878

8979
return head

plugins/pwa/plugin-pwa/src/node/options.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,23 @@ interface ApplePwaOptions {
1414
*/
1515
icon?: string
1616

17-
/**
18-
* Color of status bar
19-
*
20-
* 状态栏的颜色
21-
*
22-
* @default "black"
23-
*/
24-
statusBarColor?: 'black' | 'white'
25-
2617
/**
2718
* Safari mask icon
2819
*
2920
* Safari 图标
3021
*/
3122
maskIcon?: string
32-
}
3323

34-
interface MicrosoftPwaOptions {
3524
/**
36-
* Microsoft tile image
37-
*
38-
* 144×144 transparent recommended
25+
* @deprecated Related tag is unstandardized so you should avoid declaring it.
3926
*
40-
* 微软磁贴图片
27+
* Color of status bar
4128
*
42-
* 推荐 144×144,透明
43-
*/
44-
image?: string
45-
46-
/**
47-
* Tile color
29+
* 状态栏的颜色
4830
*
49-
* 磁贴颜色
31+
* @default "default"
5032
*/
51-
color?: string
33+
statusBarColor?: 'black-translucent' | 'black' | 'default'
5234
}
5335

5436
export interface PwaPluginOptions {
@@ -136,13 +118,6 @@ export interface PwaPluginOptions {
136118
*/
137119
apple?: ApplePwaOptions | false
138120

139-
/**
140-
* Settings for Microsoft
141-
*
142-
* 微软设置
143-
*/
144-
msTile?: MicrosoftPwaOptions | false
145-
146121
/**
147122
* Update logic
148123
*

plugins/pwa/plugin-pwa/src/node/pwaPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const pwaPlugin =
2727
'The plugin will register service worker to handle assets, so we recommend you to set "shouldPrefetch: false" in VuePress config file.',
2828
)
2929

30-
app.options.head = injectLinksToHead(options, base, app.options.head)
30+
app.options.head = injectLinksToHead(app, options)
3131

3232
return {
3333
name: PLUGIN_NAME,

0 commit comments

Comments
 (0)