Skip to content

Commit

Permalink
feat: enable prefetchPayloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Justineo committed Aug 15, 2022
1 parent 8450ac9 commit f0fee4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
21 changes: 15 additions & 6 deletions components/Hit.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ const observer =
})
: null

export function createComponent ({ isOffline, router, route }) {
export function createComponent ({ isOffline, router, route, fetchPayload }) {
return class Hit extends Component {
handleId = null;
observed = false;
link = createRef();

constructor ({ hit }) {
super()
this.url = normalizeURL(hit.url)
}

canPrefetch () {
const conn = navigator.connection
const hasBadConnection =
Expand All @@ -73,8 +78,7 @@ export function createComponent ({ isOffline, router, route }) {
}

getPrefetchComponents () {
const { hit } = this.props
const ref = router.resolve(normalizeURL(hit.url), route)
const ref = router.resolve(this.url, route)
const Components = ref.resolved.matched.map(r => r.components.default)

return Components.filter(
Expand All @@ -100,6 +104,11 @@ export function createComponent ({ isOffline, router, route }) {
}
Component.__prefetched = true
}

if (fetchPayload) {
const { href } = router.resolve(this.url, route)
fetchPayload(href, true).catch(() => {})
}
}

observe (el) {
Expand Down Expand Up @@ -134,9 +143,9 @@ export function createComponent ({ isOffline, router, route }) {
}

render ({ hit, children }) {
const url = normalizeURL(hit.url)
console.log(this.url)
return createElement('a', {
href: url,
href: this.url,
ref: this.link,
onClick: event => {
if (isSpecialClick(event)) {
Expand All @@ -156,7 +165,7 @@ export function createComponent ({ isOffline, router, route }) {
if (router.history.current.path !== hitPathname) {
event.preventDefault()
}
router.push(url)
router.push(this.url)
},
children
})
Expand Down
3 changes: 2 additions & 1 deletion components/OneSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export default {
hitComponent: createComponent({
router: this.$router,
route: this.$route,
isOffline: this.$nuxt.isOffline
isOffline: this.$nuxt.isOffline,
fetchPayload: this.$nuxt.fetchPayload
})
})
Expand Down
4 changes: 4 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = {
target: 'static',
telemetry: false,

router: {
prefetchPayloads: true
},

/**
* Headers of the page
*/
Expand Down

1 comment on commit f0fee4a

@vercel
Copy link

@vercel vercel bot commented on f0fee4a Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

veui-docs – ./

veui-docs-git-master-ecomfe.vercel.app
veui-docs-ecomfe.vercel.app
veui.vercel.app
veui.dev

Please sign in to comment.