Skip to content

Commit de2554c

Browse files
committed
docs: lazy loading
1 parent a89514d commit de2554c

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

docs/.vitepress/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/** @type {import('vitepress').UserConfig} */
22
const config = {
3+
lang: 'en-US',
4+
title: 'Vue Router',
5+
description: 'The official router for Vue.js.',
36
locales: {
47
'/': {
58
lang: 'en-US',

docs/guide/advanced/lazy-loading.md

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
# Lazy Loading Routes
22

3-
When building apps with a bundler, the JavaScript bundle can become quite large, and thus affect the page load time. It would be more efficient if we can split each route's components into a separate chunk, and only load them when the route is visited.
3+
When building apps with a bundler, the JavaScript bundle can become quite large, and thus affect the page load time. It would be more efficient if we can split each route's components into a separate chunks, and only load them when the route is visited.
44

55
<!-- TODO: fix links -->
66

7-
TODO: make it clear that this also works with other bundlers, not only with webpack
7+
<!-- TODO: make it clear that this also works with other bundlers, not only with webpack -->
88

9-
Combining Vue's [async component feature](https://vuejs.org/guide/components.html#Async-Components) and webpack's [code splitting feature](https://webpack.js.org/guides/code-splitting-async/), it's trivially easy to lazy-load route components.
9+
Vue Router supports [dynamic imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports) out of the box, meaning you can replace static imports with dynamic ones:
1010

11-
First, an async component can be defined as a factory function that returns a Promise (which should resolve to the component itself):
11+
```js
12+
// replace
13+
// import UserDetails from './views/UserDetails'
14+
// with
15+
const UserDetails = () => import('./views/UserDetails')
16+
17+
const router = createRouter({
18+
// ...
19+
routes: [{ path: '/users/:id', component: UserDetails }],
20+
})
21+
```
22+
23+
The `component` (and `components`) option accepts a function that returns a Promise of a component and Vue Router **will only fetch it when entering the page for the first time**, then use the cached version. Which means you can also have more complex functions as long as they return a Promise:
1224

1325
```js
1426
const UserDetails = () =>
@@ -17,29 +29,15 @@ const UserDetails = () =>
1729
})
1830
```
1931

20-
Second, in webpack 2, we can use the [dynamic import](https://github.com/tc39/proposal-dynamic-import) syntax to indicate a code-split point:
21-
22-
```js
23-
import('./UserDetails.vue') // returns a Promise
24-
```
32+
In general, it's a good idea **to always use dynamic imports** for all your routes.
2533

2634
::: tip Note
27-
if you are using Babel, you will need to add the [syntax-dynamic-import](https://babeljs.io/docs/plugins/syntax-dynamic-import/) plugin so that Babel can properly parse the syntax.
35+
Do **not** use [Async components](https://v3.vuejs.org/guide/component-dynamic-async.html#async-components) for routes. Async components can still be used inside route components but route component themselves are just dynamic imports.
2836
:::
2937

30-
Combining the two, this is how to define an async component that will be automatically code-split by webpack:
38+
When using a bundler like webpack, this will automatically benefit from [code splitting](https://webpack.js.org/guides/code-splitting/)
3139

32-
```js
33-
const UserDetails = () => import('./UserDetails.vue')
34-
```
35-
36-
Nothing needs to change in the route config, just use `UserDetails` as usual:
37-
38-
```js
39-
const router = createRouter({
40-
routes: [{ path: '/users/:id', component: UserDetails }]
41-
})
42-
```
40+
When using Babel, you will need to add the [syntax-dynamic-import](https://babeljs.io/docs/plugins/syntax-dynamic-import/) plugin so that Babel can properly parse the syntax.
4341

4442
## Grouping Components in the Same Chunk
4543

scripts/docs-check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# check if doc files changes for netlify
44
# needed because we cannot use && in netlify.toml
55

6-
git diff --quiet 'HEAD^' HEAD ./docs/ && ! git diff 'HEAD^' HEAD ./yarn.lock | grep --quiet vite
6+
git diff --quiet 'HEAD^' HEAD ./docs/ && ! git diff 'HEAD^' HEAD ./yarn.lock | grep --quiet vite

0 commit comments

Comments
 (0)