Skip to content

Commit

Permalink
feat(nuxt-link): Improve <n-link> and add automatic prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Dec 17, 2018
1 parent 657a6cc commit c019997
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions packages/vue-app/template/components/nuxt-link.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
<%= isTest ? '// @vue/component' : '' %>
import Vue from 'vue'

export default {
extends: Vue.component('RouterLink'),
name: 'NuxtLink',
functional: true,
render(h, { data, children }) {
return h('router-link', data, children)
props: {
noPrefetch: {
type: Boolean,
default: false
}
},
mounted() {
if (!this.noPrefetch) this.$nextTick(this.prefetch)
},
methods: {
prefetch() {
if (process.client) {
// Avoid including it in SSR bundle
var ref = this.$router.resolve(this.to, this.$route, this.append)
const Components = ref.resolved.matched.map((r) => r.components.default)
for (let i = 0; i < Components.length; i++) {
const Component = Components[i]
if (typeof Component === 'function' && !Component.__prefetched) {
try {
Component()
Component.__prefetched = true
} catch (e) {}
}
}
}
}
}
}

1 comment on commit c019997

@manniL
Copy link
Member

@manniL manniL commented on c019997 Dec 17, 2018

Choose a reason for hiding this comment

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

Oh yea 😏

Please sign in to comment.