Skip to content

Commit 2757201

Browse files
committed
docs: add cookbook for usage of client app enhance (close #113)
1 parent e7bfe49 commit 2757201

File tree

9 files changed

+181
-1
lines changed

9 files changed

+181
-1
lines changed

docs/.vuepress/configs/sidebar/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const en: SidebarConfig = {
3636
text: 'Cookbook',
3737
children: [
3838
'/advanced/cookbook/README.md',
39+
'/advanced/cookbook/usage-of-client-app-enhance.md',
3940
'/advanced/cookbook/markdown-and-vue-sfc.md',
4041
],
4142
},

docs/.vuepress/configs/sidebar/zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const zh: SidebarConfig = {
3636
text: 'Cookbook',
3737
children: [
3838
'/zh/advanced/cookbook/README.md',
39+
'/zh/advanced/cookbook/usage-of-client-app-enhance.md',
3940
'/zh/advanced/cookbook/markdown-and-vue-sfc.md',
4041
],
4142
},

docs/advanced/cookbook/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## What's the Cookbook for?
44

55
- We are introducing essential concepts in the **Guide**, but you may not know how to dig deeper.
6-
- We are listing API in the **Reference**, but you may not know how to take full advantage of them.
6+
- We are listing APIs in the **Reference**, but you may not know how to take full advantage of them.
77

88
So here comes the Cookbook.
99

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Usage of Client App Enhance
2+
3+
The [clientAppEnhanceFiles](../../reference/plugin-api.md#clientappenhancefiles) hook of Plugin API allows you to set the path to the client app enhance files. You and use it in your plugin or theme:
4+
5+
```ts
6+
const pluginOrTheme = {
7+
clientAppEnhanceFiles: path.resolve(__dirname, './path/to/clientAppEnhance.ts'),
8+
}
9+
```
10+
11+
Then create a `clientAppEnhance.ts` file. You can make use of the [defineClientAppEnhance](../../reference/client-api.md#defineclientappenhance) helper to get the types hint. Notice that the function can be either synchronous or asynchronous.
12+
13+
```ts
14+
import { defineClientAppEnhance } from '@vuepress/client'
15+
16+
export default defineClientAppEnhance(({ app, router, siteData }) => {
17+
// ...
18+
})
19+
```
20+
21+
- The `app` is the Vue application instance that created by [createApp](https://v3.vuejs.org/api/application-api.html).
22+
- The `router` is the Vue Router instance that created by [createRouter](https://next.router.vuejs.org/api/#createrouter).
23+
- The `siteData` is an object that generated from user config, including [base](../../reference/config.md#base), [lang](../../reference/config.md#lang), [title](../../reference/config.md#title), [description](../../reference/config.md#description), [head](../../reference/config.md#head) and [locales](../../reference/config.md#locales).
24+
25+
The client app enhance will be invoked after the client app is created. It's possible to implement any enhancements to the Vue application.
26+
27+
::: tip
28+
For ease of use in user config, the `.vuepress/clientAppEnhance.{js,ts}` file will be used as the client app enhance file implicitly, unless you set `clientAppEnhanceFiles` explicitly in the config file.
29+
:::
30+
31+
## Register Vue Components
32+
33+
You can register global Vue components via the [component](https://v3.vuejs.org/api/application-api.html#component) method:
34+
35+
```ts
36+
import { defineClientAppEnhance } from '@vuepress/client'
37+
import MyComponent from './MyComponent.vue'
38+
39+
export default defineClientAppEnhance(({ app, router, siteData }) => {
40+
app.component('MyComponent', MyComponent)
41+
})
42+
```
43+
44+
## Use Non-SSR-Friendly Features
45+
46+
VuePress will generate a SSR application to pre-render pages during build. Generally speaking, if a code snippet is using Browser / DOM APIs before client app is mounted, we call it non-SSR-friendly.
47+
48+
We already provides a [ClientOnly](../../reference/components.md#clientonly) component to wrap non-SSR-friendly content.
49+
50+
In client app enhance files, you can make use of the [`__SSR__`](../../reference/client-api.md#ssr) flag for that purpose.
51+
52+
```ts
53+
import { defineClientAppEnhance } from '@vuepress/client'
54+
55+
export default defineClientAppEnhance(async ({ app, router, siteData }) => {
56+
if (!__SSR__) {
57+
const nonSsrFriendlyModule = await import('non-ssr-friendly-module')
58+
// ...
59+
}
60+
})
61+
```
62+
63+
## Use Router Methods
64+
65+
You can make use of the [Router Methods](https://next.router.vuejs.org/api/#router-methods) that provided by vue-router. For example, add navigation guard:
66+
67+
```ts
68+
import { defineClientAppEnhance } from '@vuepress/client'
69+
70+
export default defineClientAppEnhance(({ app, router, siteData }) => {
71+
router.beforeEach((to) => {
72+
console.log('before navigation')
73+
})
74+
75+
router.afterEach((to) => {
76+
console.log('after navigation')
77+
})
78+
})
79+
```
80+
81+
::: warning
82+
It not recommended to use `addRoute` method to add dynamic routes here, because those routes will **NOT** be pre-rendered in build mode.
83+
84+
But you can still do that if you understand the drawback.
85+
:::

docs/reference/client-api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export default defineClientAppEnhance(({ app, router, siteData }) => {
8484
})
8585
```
8686

87+
- Also see:
88+
- [Cookbook > Usage of Client App Enhance](../advanced/cookbook/usage-of-client-app-enhance.md)
89+
8790
### defineClientAppSetup
8891

8992
- Details:

docs/reference/plugin-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ module.exports = {
257257

258258
- Also see:
259259
- [Client API > defineClientAppEnhance](./client-api.md#defineclientappenhance)
260+
- [Cookbook > Usage of Client App Enhance](../advanced/cookbook/usage-of-client-app-enhance.md)
260261

261262
### clientAppRootComponentFiles
262263

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Client App Enhance 的使用方法
2+
3+
Plugin API 提供的 [clientAppEnhanceFiles](../../reference/plugin-api.md#clientappenhancefiles) Hook 允许你设置 Client App Enhance 文件的路径。你可以在你的插件或者主题中使用它:
4+
5+
```ts
6+
const pluginOrTheme = {
7+
clientAppEnhanceFiles: path.resolve(__dirname, './path/to/clientAppEnhance.ts'),
8+
}
9+
```
10+
11+
然后,创建 `clientAppEnhance.ts` 文件。你可以使用 [defineClientAppEnhance](../../reference/client-api.md#defineclientappenhance) Helper 来获取类型提示。注意这个函数既可以是同步的,也可以是异步的。
12+
13+
```ts
14+
import { defineClientAppEnhance } from '@vuepress/client'
15+
16+
export default defineClientAppEnhance(({ app, router, siteData }) => {
17+
// ...
18+
})
19+
```
20+
21+
- `app` 是由 [createApp](https://v3.cn.vuejs.org/api/application-api.html) 创建的 Vue 应用实例。
22+
- `router` 是由 [createRouter](https://next.router.vuejs.org/zh/api/index.html#createrouter) 创建的路由实例。
23+
- `siteData` 是一个根据用户配置生成的对象,包含 base](../../reference/config.md#base), [lang](../../reference/config.md#lang), [title](../../reference/config.md#title), [description](../../reference/config.md#description), [head](../../reference/config.md#head)[locales](../../reference/config.md#locales)
24+
25+
Client App Enhance 会在客户端应用创建后被调用,它可以为 Vue 应用添加任意功能。
26+
27+
::: tip
28+
为了方便用户配置使用, `.vuepress/clientAppEnhance.{js,ts}` 文件会被隐式地用作 Client App Enhance 文件,除非你在配置文件中显式设置了 `clientAppEnhanceFiles`
29+
:::
30+
31+
## 注册 Vue 组件
32+
33+
你可以通过 [component](https://v3.cn.vuejs.org/api/application-api.html#component) 方法来注册 Vue 全局组件:
34+
35+
```ts
36+
import { defineClientAppEnhance } from '@vuepress/client'
37+
import MyComponent from './MyComponent.vue'
38+
39+
export default defineClientAppEnhance(({ app, router, siteData }) => {
40+
app.component('MyComponent', MyComponent)
41+
})
42+
```
43+
44+
## 使用不支持 SSR 的功能
45+
46+
VuePress 会在构建过程中生成一个 SSR 应用,用以对页面进行预渲染。一般而言,如果一段代码在客户端应用 Mount 之前就使用了浏览器或 DOM API ,我们就认为其对 SSR 不友好,即不支持 SSR 。
47+
48+
我们已经提供了一个 [ClientOnly](../../reference/components.md#clientonly) 组件来包裹不支持 SSR 的内容。
49+
50+
在 Client App Enhance 文件中,你可以使用 [`__SSR__`](../../reference/client-api.md#ssr) 标记来处理这种情况。
51+
52+
```ts
53+
import { defineClientAppEnhance } from '@vuepress/client'
54+
55+
export default defineClientAppEnhance(async ({ app, router, siteData }) => {
56+
if (!__SSR__) {
57+
const nonSsrFriendlyModule = await import('non-ssr-friendly-module')
58+
// ...
59+
}
60+
})
61+
```
62+
63+
## 使用 Router 方法
64+
65+
你可以使用 vue-router 提供的 [Router 方法](https://next.router.vuejs.org/zh/api/index.html#router-方法) 。例如,添加导航钩子:
66+
67+
```ts
68+
import { defineClientAppEnhance } from '@vuepress/client'
69+
70+
export default defineClientAppEnhance(({ app, router, siteData }) => {
71+
router.beforeEach((to) => {
72+
console.log('before navigation')
73+
})
74+
75+
router.afterEach((to) => {
76+
console.log('after navigation')
77+
})
78+
})
79+
```
80+
81+
::: warning
82+
我们不推荐使用 `addRoute` 方法来添加动态路由,因为这些路由记录 **不会** 在构建模式中被预渲染出来。
83+
84+
当然,如果你了解了这种用法的缺点,你还是可以这样使用。
85+
:::

docs/zh/reference/client-api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export default defineClientAppEnhance(({ app, router, siteData }) => {
8484
})
8585
```
8686

87+
- 参考:
88+
- [Cookbook > Client App Enhance 的使用方法](../advanced/cookbook/usage-of-client-app-enhance.md)
89+
8790
### defineClientAppSetup
8891

8992
- 详情:

docs/zh/reference/plugin-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ module.exports = {
257257

258258
- 参考:
259259
- [客户端 API > defineClientAppEnhance](./client-api.md#defineclientappenhance)
260+
- [Cookbook > Client App Enhance 的使用方法](../advanced/cookbook/usage-of-client-app-enhance.md)
260261

261262
### clientAppRootComponentFiles
262263

0 commit comments

Comments
 (0)