2018-12-28 16:27:03 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
|
|
|
const requestIdleCallback = window.requestIdleCallback ||
|
|
|
|
function (cb) {
|
|
|
|
const start = Date.now()
|
|
|
|
return setTimeout(function () {
|
|
|
|
cb({
|
|
|
|
didTimeout: false,
|
2019-09-10 09:51:14 +00:00
|
|
|
timeRemaining: () => Math.max(0, 50 - (Date.now() - start))
|
2018-12-28 16:27:03 +00:00
|
|
|
})
|
|
|
|
}, 1)
|
|
|
|
}
|
2019-09-18 08:54:17 +00:00
|
|
|
|
|
|
|
const cancelIdleCallback = window.cancelIdleCallback || function (id) {
|
|
|
|
clearTimeout(id)
|
|
|
|
}
|
|
|
|
|
2019-01-06 07:56:59 +00:00
|
|
|
const observer = window.IntersectionObserver && new window.IntersectionObserver((entries) => {
|
2018-12-28 16:27:03 +00:00
|
|
|
entries.forEach(({ intersectionRatio, target: link }) => {
|
|
|
|
if (intersectionRatio <= 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
link.__prefetch()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-01-06 07:56:59 +00:00
|
|
|
<%= isTest ? '// @vue/component' : '' %>
|
2018-12-28 16:27:03 +00:00
|
|
|
export default {
|
|
|
|
name: 'NuxtLink',
|
2019-01-06 07:56:59 +00:00
|
|
|
extends: Vue.component('RouterLink'),
|
2018-12-28 16:27:03 +00:00
|
|
|
props: {
|
2019-09-18 15:06:46 +00:00
|
|
|
prefetch: {
|
|
|
|
type: Boolean,
|
|
|
|
default: <%= router.prefetchLinks ? 'true' : 'false' %>
|
|
|
|
},
|
2018-12-28 16:27:03 +00:00
|
|
|
noPrefetch: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
}<% if (router.linkPrefetchedClass) { %>,
|
|
|
|
prefetchedClass: {
|
|
|
|
type: String,
|
|
|
|
default: '<%= router.linkPrefetchedClass %>'
|
|
|
|
}<% } %>
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
mounted () {
|
2019-09-18 15:06:46 +00:00
|
|
|
if (this.prefetch && !this.noPrefetch) {
|
2019-09-18 08:54:17 +00:00
|
|
|
this.handleId = requestIdleCallback(this.observe, { timeout: 2e3 })
|
2018-12-28 16:27:03 +00:00
|
|
|
}
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
beforeDestroy () {
|
2019-09-18 08:54:17 +00:00
|
|
|
cancelIdleCallback(this.handleId)
|
|
|
|
|
2018-12-28 16:27:03 +00:00
|
|
|
if (this.__observed) {
|
|
|
|
observer.unobserve(this.$el)
|
|
|
|
delete this.$el.__prefetch
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-09-10 09:51:14 +00:00
|
|
|
observe () {
|
2018-12-28 16:27:03 +00:00
|
|
|
// If no IntersectionObserver, avoid prefetching
|
|
|
|
if (!observer) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Add to observer
|
|
|
|
if (this.shouldPrefetch()) {
|
2019-09-18 15:06:46 +00:00
|
|
|
this.$el.__prefetch = this.prefetchLink.bind(this)
|
2018-12-28 16:27:03 +00:00
|
|
|
observer.observe(this.$el)
|
|
|
|
this.__observed = true
|
|
|
|
}<% if (router.linkPrefetchedClass) { %> else {
|
|
|
|
this.addPrefetchedClass()
|
|
|
|
}<% } %>
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
shouldPrefetch () {
|
2018-12-28 16:27:03 +00:00
|
|
|
return this.getPrefetchComponents().length > 0
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
canPrefetch () {
|
2018-12-28 16:27:03 +00:00
|
|
|
const conn = navigator.connection
|
2019-01-21 13:23:07 +00:00
|
|
|
const hasBadConnection = this.<%= globals.nuxt %>.isOffline || (conn && ((conn.effectiveType || '').includes('2g') || conn.saveData))
|
2018-12-28 16:27:03 +00:00
|
|
|
|
|
|
|
return !hasBadConnection
|
|
|
|
},
|
2019-09-10 09:51:14 +00:00
|
|
|
getPrefetchComponents () {
|
2018-12-28 16:27:03 +00:00
|
|
|
const ref = this.$router.resolve(this.to, this.$route, this.append)
|
2019-01-06 07:56:59 +00:00
|
|
|
const Components = ref.resolved.matched.map(r => r.components.default)
|
2018-12-28 16:27:03 +00:00
|
|
|
|
2019-01-06 07:56:59 +00:00
|
|
|
return Components.filter(Component => typeof Component === 'function' && !Component.options && !Component.__prefetched)
|
2018-12-28 16:27:03 +00:00
|
|
|
},
|
2019-09-18 15:06:46 +00:00
|
|
|
prefetchLink () {
|
2018-12-28 16:27:03 +00:00
|
|
|
if (!this.canPrefetch()) {
|
|
|
|
return
|
|
|
|
}
|
2019-09-18 08:54:17 +00:00
|
|
|
// Stop observing this link (in case of internet connection changes)
|
2018-12-28 16:27:03 +00:00
|
|
|
observer.unobserve(this.$el)
|
|
|
|
const Components = this.getPrefetchComponents()
|
|
|
|
|
|
|
|
for (const Component of Components) {
|
2019-05-16 19:31:12 +00:00
|
|
|
const componentOrPromise = Component()
|
|
|
|
if (componentOrPromise instanceof Promise) {
|
|
|
|
componentOrPromise.catch(() => {})
|
|
|
|
}
|
2019-05-10 09:56:28 +00:00
|
|
|
Component.__prefetched = true
|
2018-12-28 16:27:03 +00:00
|
|
|
}<% if (router.linkPrefetchedClass) { %>
|
|
|
|
this.addPrefetchedClass()<% } %>
|
|
|
|
}<% if (router.linkPrefetchedClass) { %>,
|
2019-09-10 09:51:14 +00:00
|
|
|
addPrefetchedClass () {
|
2018-12-28 16:27:03 +00:00
|
|
|
if (this.prefetchedClass !== 'false') {
|
2019-01-30 11:34:33 +00:00
|
|
|
this.$el.className = (this.$el.className + ' ' + this.prefetchedClass).trim()
|
2018-12-28 16:27:03 +00:00
|
|
|
}
|
|
|
|
}<% } %>
|
|
|
|
}
|
|
|
|
}
|