Skip to content

Commit f0fee4a

Browse files
committed
feat: enable prefetchPayloads
1 parent 8450ac9 commit f0fee4a

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

components/Hit.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ const observer =
5757
})
5858
: null
5959

60-
export function createComponent ({ isOffline, router, route }) {
60+
export function createComponent ({ isOffline, router, route, fetchPayload }) {
6161
return class Hit extends Component {
6262
handleId = null;
6363
observed = false;
6464
link = createRef();
6565

66+
constructor ({ hit }) {
67+
super()
68+
this.url = normalizeURL(hit.url)
69+
}
70+
6671
canPrefetch () {
6772
const conn = navigator.connection
6873
const hasBadConnection =
@@ -73,8 +78,7 @@ export function createComponent ({ isOffline, router, route }) {
7378
}
7479

7580
getPrefetchComponents () {
76-
const { hit } = this.props
77-
const ref = router.resolve(normalizeURL(hit.url), route)
81+
const ref = router.resolve(this.url, route)
7882
const Components = ref.resolved.matched.map(r => r.components.default)
7983

8084
return Components.filter(
@@ -100,6 +104,11 @@ export function createComponent ({ isOffline, router, route }) {
100104
}
101105
Component.__prefetched = true
102106
}
107+
108+
if (fetchPayload) {
109+
const { href } = router.resolve(this.url, route)
110+
fetchPayload(href, true).catch(() => {})
111+
}
103112
}
104113

105114
observe (el) {
@@ -134,9 +143,9 @@ export function createComponent ({ isOffline, router, route }) {
134143
}
135144

136145
render ({ hit, children }) {
137-
const url = normalizeURL(hit.url)
146+
console.log(this.url)
138147
return createElement('a', {
139-
href: url,
148+
href: this.url,
140149
ref: this.link,
141150
onClick: event => {
142151
if (isSpecialClick(event)) {
@@ -156,7 +165,7 @@ export function createComponent ({ isOffline, router, route }) {
156165
if (router.history.current.path !== hitPathname) {
157166
event.preventDefault()
158167
}
159-
router.push(url)
168+
router.push(this.url)
160169
},
161170
children
162171
})

components/OneSearch.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export default {
9595
hitComponent: createComponent({
9696
router: this.$router,
9797
route: this.$route,
98-
isOffline: this.$nuxt.isOffline
98+
isOffline: this.$nuxt.isOffline,
99+
fetchPayload: this.$nuxt.fetchPayload
99100
})
100101
})
101102

nuxt.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ module.exports = {
1313
target: 'static',
1414
telemetry: false,
1515

16+
router: {
17+
prefetchPayloads: true
18+
},
19+
1620
/**
1721
* Headers of the page
1822
*/

0 commit comments

Comments
 (0)