Skip to content

Commit e39e99b

Browse files
committed
feat: support prefetch and prefetchFiles on route object
1 parent 8ef6171 commit e39e99b

File tree

6 files changed

+106
-10
lines changed

6 files changed

+106
-10
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,41 @@ All [props](https://router.vuejs.org/api/#router-link-props) of `<router-link>`
5959

6060
Whether to prefetch the matched route component.
6161

62+
You can also set `meta.prefetch` on vue-router's `route` object to disable prefetching this route for all `<router-link>`s:
63+
64+
```js
65+
new VueRouter({
66+
routes: [
67+
{
68+
path: '/some-async-page',
69+
meta: { prefetch: false },
70+
component: () => import('./async-page.vue')
71+
}
72+
]
73+
})
74+
```
75+
6276
### prefetchFiles
6377

6478
- Type: `string[]`
6579
- Examples: `['/foo.css']`
6680

6781
A list of additional files to prefetch. By default we only prefetch the route component.
6882

83+
You can also set `meta.prefetchFiles` on vue-router's `route` object, it will be merged with the prop value:
84+
85+
```js
86+
new VueRouter({
87+
routes: [
88+
{
89+
path: '/some-async-page',
90+
meta: { prefetchFiles: ['/foo.css'] },
91+
component: () => import('./async-page.vue')
92+
}
93+
]
94+
})
95+
```
96+
6997
### timeout
7098

7199
- Type: `number`

example/async-page.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div>
3+
<h1>Async Page</h1>
4+
<router-link to="/">Home</router-link>
5+
</div>
6+
</template>

example/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ const Nav = {
2929
<li>
3030
<router-link to="/page/5">page 5</router-link>
3131
</li>
32+
<li>
33+
<router-link to="/async-page">async-page</router-link>
34+
</li>
3235
<li>
3336
<router-link to="/page/6">page 6</router-link>
3437
</li>
38+
<li>
39+
<router-link to="/page/1">page 1</router-link>
40+
</li>
3541
</ul>
3642
</div>
3743
)
@@ -66,6 +72,16 @@ const router = new Router({
6672
)
6773
}
6874
}
75+
},
76+
{
77+
path: '/async-page',
78+
meta: {
79+
prefetch(route) {
80+
console.log(route)
81+
}
82+
},
83+
component: () =>
84+
import(/* webpackChunkName: "async-page" */ './async-page.vue')
6985
}
7086
]
7187
})

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"author": "egoist<[email protected]>",
2323
"license": "MIT",
2424
"devDependencies": {
25+
"babel-eslint": "^10.0.1",
2526
"bili": "^4.0.9",
2627
"commitizen": "^3.0.5",
2728
"cz-conventional-changelog": "^2.1.0",
@@ -40,6 +41,11 @@
4041
"xo": "^0.23.0"
4142
},
4243
"xo": {
44+
"parserOptions": {
45+
"parser": "babel-eslint",
46+
"ecmaVersion": 2017,
47+
"sourceType": "module"
48+
},
4349
"extends": [
4450
"rem",
4551
"plugin:vue/recommended",

src/index.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,36 @@ function installRouterPrefetch(Vue, { componentName = 'RouterLink' } = {}) {
7171
},
7272
getComponents() {
7373
return this.$router.getMatchedComponents(this.to).filter(Component => {
74-
return typeof Component === 'function' && !Component._prefetched
74+
return typeof Component === 'function'
7575
})
7676
},
7777
linkPrefetch() {
78-
// Prefetch route component
79-
const components = this.getComponents()
80-
for (const Component of components) {
81-
this.$emit('prefetch', this.to)
82-
Component() // eslint-disable-line new-cap
83-
Component._prefetched = true
78+
const { route } = this.$router.resolve(this.to)
79+
80+
if (route.meta.__prefetched) return
81+
82+
route.meta.__prefetched = true
83+
84+
if (route.meta.prefetch !== false) {
85+
// Prefetch route component
86+
const components = this.getComponents()
87+
for (const Component of components) {
88+
this.$emit('prefetch', this.to)
89+
Component() // eslint-disable-line new-cap
90+
}
91+
}
92+
93+
if (typeof route.meta.prefetch === 'function') {
94+
route.meta.prefetch(route)
8495
}
8596

8697
// Prefetch addtional files
87-
if (this.prefetchFiles) {
88-
for (const file of this.prefetchFiles) {
98+
const prefetchFiles = [
99+
...(this.prefetchFiles || []),
100+
...(route.meta.prefetchFiles || [])
101+
]
102+
if (prefetchFiles.length > 0) {
103+
for (const file of prefetchFiles) {
89104
prefetch(file)
90105
}
91106
}

yarn.lock

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@
232232
esutils "^2.0.2"
233233
js-tokens "^4.0.0"
234234

235+
"@babel/parser@^7.0.0":
236+
version "7.3.2"
237+
resolved "https://registry.npmjs.org/@babel/parser/-/parser-7.3.2.tgz#95cdeddfc3992a6ca2a1315191c1679ca32c55cd"
238+
integrity sha512-QzNUC2RO1gadg+fs21fi0Uu0OuGNzRKEmgCxoLNzbCdoprLwjfmZwzUrpUNfJPaVRwBpDY47A17yYEGWyRelnQ==
239+
235240
"@babel/parser@^7.2.2", "@babel/parser@^7.2.3":
236241
version "7.3.1"
237242
resolved "https://registry.npmjs.org/@babel/parser/-/parser-7.3.1.tgz#8f4ffd45f779e6132780835ffa7a215fa0b2d181"
@@ -707,7 +712,7 @@
707712
"@babel/parser" "^7.2.2"
708713
"@babel/types" "^7.2.2"
709714

710-
"@babel/traverse@^7.1.0", "@babel/traverse@^7.1.5", "@babel/traverse@^7.2.2", "@babel/traverse@^7.2.3":
715+
"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0", "@babel/traverse@^7.1.5", "@babel/traverse@^7.2.2", "@babel/traverse@^7.2.3":
711716
version "7.2.3"
712717
resolved "https://registry.npmjs.org/@babel/traverse/-/traverse-7.2.3.tgz#7ff50cefa9c7c0bd2d81231fdac122f3957748d8"
713718
integrity sha512-Z31oUD/fJvEWVR0lNZtfgvVt512ForCTNKYcJBGbPb1QZfve4WGH8Wsy7+Mev33/45fhP/hwQtvgusNdcCMgSw==
@@ -1457,6 +1462,18 @@ babel-code-frame@^6.26.0:
14571462
esutils "^2.0.2"
14581463
js-tokens "^3.0.2"
14591464

1465+
babel-eslint@^10.0.1:
1466+
version "10.0.1"
1467+
resolved "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.0.1.tgz#919681dc099614cd7d31d45c8908695092a1faed"
1468+
integrity sha512-z7OT1iNV+TjOwHNLLyJk+HN+YVWX+CLE6fPD2SymJZOZQBs+QIexFjhm4keGTm8MW9xr4EC9Q0PbaLB24V5GoQ==
1469+
dependencies:
1470+
"@babel/code-frame" "^7.0.0"
1471+
"@babel/parser" "^7.0.0"
1472+
"@babel/traverse" "^7.0.0"
1473+
"@babel/types" "^7.0.0"
1474+
eslint-scope "3.7.1"
1475+
eslint-visitor-keys "^1.0.0"
1476+
14601477
babel-helper-vue-jsx-merge-props@^2.0.3:
14611478
version "2.0.3"
14621479
resolved "https://registry.npmjs.org/babel-helper-vue-jsx-merge-props/-/babel-helper-vue-jsx-merge-props-2.0.3.tgz#22aebd3b33902328e513293a8e4992b384f9f1b6"
@@ -3669,6 +3686,14 @@ eslint-plugin-vue@^5.1.0:
36693686
dependencies:
36703687
vue-eslint-parser "^4.0.2"
36713688

3689+
3690+
version "3.7.1"
3691+
resolved "https://registry.npmjs.org/eslint-scope/-/eslint-scope-3.7.1.tgz#3d63c3edfda02e06e01a452ad88caacc7cdcb6e8"
3692+
integrity sha1-PWPD7f2gLgbgGkUq2IyqzHzctug=
3693+
dependencies:
3694+
esrecurse "^4.1.0"
3695+
estraverse "^4.1.1"
3696+
36723697
eslint-scope@^4.0.0:
36733698
version "4.0.0"
36743699
resolved "https://registry.npmjs.org/eslint-scope/-/eslint-scope-4.0.0.tgz#50bf3071e9338bcdc43331794a0cb533f0136172"

0 commit comments

Comments
 (0)