Skip to content

Commit de9eb07

Browse files
docs: update Lazy Loading docs (#2618)
1 parent 8d030c7 commit de9eb07

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

packages/docs/guide/advanced/lazy-loading.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ Vue Router supports [dynamic imports](https://developer.mozilla.org/en-US/docs/W
1111

1212
```js
1313
// replace
14-
// import UserDetails from './views/UserDetails'
14+
// import UserDetails from './views/UserDetails.vue'
1515
// with
1616
const UserDetails = () => import('./views/UserDetails.vue')
1717

1818
const router = createRouter({
1919
// ...
2020
routes: [
21-
{ path: '/users/:id', component: UserDetails }
21+
{ path: '/users/:id', component: UserDetails },
2222
// or use it directly in the route definition
2323
{ path: '/users/:id', component: () => import('./views/UserDetails.vue') },
2424
],
@@ -27,8 +27,6 @@ const router = createRouter({
2727

2828
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:
2929

30-
<RuleKitLink />
31-
3230
```js
3331
const UserDetails = () =>
3432
Promise.resolve({
@@ -38,34 +36,32 @@ const UserDetails = () =>
3836

3937
In general, it's a good idea **to always use dynamic imports** for all your routes.
4038

41-
::: tip Note
42-
Do **not** use [Async components](https://vuejs.org/guide/components/async.html) for routes. Async components can still be used inside route components but route component themselves are just dynamic imports.
43-
:::
39+
When using a bundler like Vite or webpack, this will automatically benefit from [code splitting](https://webpack.js.org/guides/code-splitting/).
4440

45-
When using a bundler like webpack, this will automatically benefit from [code splitting](https://webpack.js.org/guides/code-splitting/)
41+
<RuleKitLink />
4642

47-
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.
43+
## Relationship to async components
4844

49-
## Grouping Components in the Same Chunk
45+
Vue Router's lazy loading may appear similar to Vue's [async components](https://vuejs.org/guide/components/async.html), but they are distinct features. Do **not** use async components as route components. An async component can still be used inside a route component but the route component itself should just be a function.
5046

51-
### With webpack
47+
## Relationship to functional components
5248

53-
Sometimes we may want to group all the components nested under the same route into the same async chunk. To achieve that we need to use [named chunks](https://webpack.js.org/guides/code-splitting/#dynamic-imports) by providing a chunk name using a special comment syntax (requires webpack > 2.4):
49+
While not common, it is possible to use a [functional component](https://vuejs.org/guide/extras/render-function.html#functional-components) as a route component. However, Vue Router needs some way to differentiate between functional components and lazy loading. To use a functional component we must give the function a `displayName`:
5450

55-
```js
56-
const UserDetails = () =>
57-
import(/* webpackChunkName: "group-user" */ './UserDetails.vue')
58-
const UserDashboard = () =>
59-
import(/* webpackChunkName: "group-user" */ './UserDashboard.vue')
60-
const UserProfileEdit = () =>
61-
import(/* webpackChunkName: "group-user" */ './UserProfileEdit.vue')
51+
```ts
52+
const AboutPage: FunctionalComponent = () => {
53+
return h('h1', {}, 'About')
54+
}
55+
AboutPage.displayName = 'AboutPage'
6256
```
6357

64-
webpack will group any async module with the same chunk name into the same async chunk.
58+
## Grouping Components in the Same Chunk
59+
60+
We may want to group all the components nested under the same route into the same chunk, so they can all be loaded with a single request.
6561

6662
### With Vite
6763

68-
In Vite you can define the chunks under the [`rollupOptions`](https://vite.dev/config/build-options.html#build-rollupoptions):
64+
We can define the chunks under the [`rollupOptions`](https://vite.dev/config/build-options.html#build-rollupoptions):
6965

7066
```js [vite.config.js]
7167
export default defineConfig({
@@ -85,3 +81,18 @@ export default defineConfig({
8581
},
8682
})
8783
```
84+
85+
### With webpack
86+
87+
We can specify the [chunk name](https://webpack.js.org/api/module-methods/#webpackchunkname) using a special comment syntax:
88+
89+
```js
90+
const UserDetails = () =>
91+
import(/* webpackChunkName: "group-user" */ './UserDetails.vue')
92+
const UserDashboard = () =>
93+
import(/* webpackChunkName: "group-user" */ './UserDashboard.vue')
94+
const UserProfileEdit = () =>
95+
import(/* webpackChunkName: "group-user" */ './UserProfileEdit.vue')
96+
```
97+
98+
webpack will group any async module with the same chunk name into the same async chunk.

packages/docs/guide/index.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,6 @@ export const router = createRouter({
7171

7272
The `routes` option defines the routes themselves, mapping URL paths to components. The component specified by the `component` option is the one that will be rendered by the `<RouterView>` in our earlier `App.vue`. These route components are sometimes referred to as _views_, though they are just normal Vue components.
7373

74-
It's worth noting that if you want to use _functional components_ as route components, you must give them a `displayName` so they can be differentiated from [lazy loaded routes](./advanced/lazy-loading.md):
75-
76-
```ts
77-
const AboutPage: FunctionalComponent = () => {
78-
return h('h1', {}, 'About')
79-
}
80-
AboutPage.displayName = 'AboutPage'
81-
```
82-
8374
Routes support various other options that we'll see later in the guide, but for now we only need `path` and `component`.
8475

8576
The `history` option controls how routes are mapped onto URLs and vice versa. For the Playground example we're using `createMemoryHistory()`, which ignores the browser URL entirely and uses its own internal URL instead. That works well for the Playground, but it's unlikely to be what you'd want in a real application. Typically, you'd want to use `createWebHistory()` instead, or perhaps `createWebHashHistory()`. We'll cover that topic in more detail in the guide to [History modes](./essentials/history-mode).

0 commit comments

Comments
 (0)