2018-11-24 23:47:39 +00:00
|
|
|
/* global document */
|
2019-01-04 20:49:21 +00:00
|
|
|
import mitt from 'next-server/dist/lib/mitt'
|
2017-04-11 16:37:59 +00:00
|
|
|
|
2018-11-24 23:47:39 +00:00
|
|
|
// smaller version of https://gist.github.com/igrigorik/a02f2359f3bc50ca7a9c
|
2018-11-26 22:58:40 +00:00
|
|
|
function supportsPreload (list) {
|
2018-11-24 23:47:39 +00:00
|
|
|
if (!list || !list.supports) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
try {
|
2018-11-26 22:58:40 +00:00
|
|
|
return list.supports('preload')
|
2018-11-24 23:47:39 +00:00
|
|
|
} catch (e) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:58:40 +00:00
|
|
|
const hasPreload = supportsPreload(document.createElement('link').relList)
|
2017-04-06 06:49:00 +00:00
|
|
|
|
2017-04-03 18:10:24 +00:00
|
|
|
export default class PageLoader {
|
2017-04-18 04:18:43 +00:00
|
|
|
constructor (buildId, assetPrefix) {
|
2017-04-03 18:10:24 +00:00
|
|
|
this.buildId = buildId
|
2017-04-18 04:18:43 +00:00
|
|
|
this.assetPrefix = assetPrefix
|
|
|
|
|
2017-04-03 18:10:24 +00:00
|
|
|
this.pageCache = {}
|
2018-11-24 23:47:39 +00:00
|
|
|
this.prefetchCache = new Set()
|
2019-01-04 20:49:21 +00:00
|
|
|
this.pageRegisterEvents = mitt()
|
2017-04-03 18:10:24 +00:00
|
|
|
this.loadingRoutes = {}
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:55:56 +00:00
|
|
|
normalizeRoute (route) {
|
2017-04-03 18:10:24 +00:00
|
|
|
if (route[0] !== '/') {
|
2017-06-20 19:43:38 +00:00
|
|
|
throw new Error(`Route name should start with a "/", got "${route}"`)
|
2017-04-03 18:10:24 +00:00
|
|
|
}
|
2017-07-09 04:20:30 +00:00
|
|
|
route = route.replace(/\/index$/, '/')
|
2017-04-03 18:10:24 +00:00
|
|
|
|
2017-05-01 23:26:18 +00:00
|
|
|
if (route === '/') return route
|
2017-05-04 20:05:47 +00:00
|
|
|
return route.replace(/\/$/, '')
|
2017-04-04 19:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loadPage (route) {
|
|
|
|
route = this.normalizeRoute(route)
|
2017-04-03 18:10:24 +00:00
|
|
|
|
2017-04-11 16:37:59 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const fire = ({ error, page }) => {
|
2017-04-17 20:15:50 +00:00
|
|
|
this.pageRegisterEvents.off(route, fire)
|
2017-06-23 05:16:55 +00:00
|
|
|
delete this.loadingRoutes[route]
|
2017-04-03 18:10:24 +00:00
|
|
|
|
2017-04-11 16:37:59 +00:00
|
|
|
if (error) {
|
|
|
|
reject(error)
|
|
|
|
} else {
|
|
|
|
resolve(page)
|
|
|
|
}
|
|
|
|
}
|
2017-04-03 18:10:24 +00:00
|
|
|
|
2017-06-23 05:16:55 +00:00
|
|
|
// If there's a cached version of the page, let's use it.
|
|
|
|
const cachedPage = this.pageCache[route]
|
|
|
|
if (cachedPage) {
|
|
|
|
const { error, page } = cachedPage
|
|
|
|
error ? reject(error) : resolve(page)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a listener to get the page
|
2017-04-17 20:15:50 +00:00
|
|
|
this.pageRegisterEvents.on(route, fire)
|
2017-04-03 18:10:24 +00:00
|
|
|
|
2017-05-09 01:20:50 +00:00
|
|
|
// If the page is loading via SSR, we need to wait for it
|
|
|
|
// rather downloading it again.
|
|
|
|
if (document.getElementById(`__NEXT_PAGE__${route}`)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:37:59 +00:00
|
|
|
// Load the script if not asked to load yet.
|
|
|
|
if (!this.loadingRoutes[route]) {
|
|
|
|
this.loadScript(route)
|
|
|
|
this.loadingRoutes[route] = true
|
|
|
|
}
|
2017-04-03 18:10:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:37:59 +00:00
|
|
|
loadScript (route) {
|
2017-04-04 19:55:56 +00:00
|
|
|
route = this.normalizeRoute(route)
|
2018-01-13 07:34:48 +00:00
|
|
|
const scriptRoute = route === '/' ? '/index.js' : `${route}.js`
|
2017-05-11 15:24:27 +00:00
|
|
|
|
2017-04-03 18:10:24 +00:00
|
|
|
const script = document.createElement('script')
|
2018-07-25 11:45:42 +00:00
|
|
|
const url = `${this.assetPrefix}/_next/static/${encodeURIComponent(this.buildId)}/pages${scriptRoute}`
|
2018-12-13 00:05:21 +00:00
|
|
|
script.crossOrigin = process.crossOrigin
|
2017-04-03 18:10:24 +00:00
|
|
|
script.src = url
|
2017-04-11 16:37:59 +00:00
|
|
|
script.onerror = () => {
|
|
|
|
const error = new Error(`Error when loading route: ${route}`)
|
2018-02-21 17:41:25 +00:00
|
|
|
error.code = 'PAGE_LOAD_ERROR'
|
2017-04-17 20:15:50 +00:00
|
|
|
this.pageRegisterEvents.emit(route, { error })
|
2017-04-03 18:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
document.body.appendChild(script)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method if called by the route code.
|
2017-04-06 06:49:00 +00:00
|
|
|
registerPage (route, regFn) {
|
2017-04-11 14:33:18 +00:00
|
|
|
const register = () => {
|
2017-06-16 11:13:55 +00:00
|
|
|
try {
|
|
|
|
const { error, page } = regFn()
|
|
|
|
this.pageCache[route] = { error, page }
|
|
|
|
this.pageRegisterEvents.emit(route, { error, page })
|
|
|
|
} catch (error) {
|
|
|
|
this.pageCache[route] = { error }
|
|
|
|
this.pageRegisterEvents.emit(route, { error })
|
|
|
|
}
|
2017-04-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 18:06:36 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
// Wait for webpack to become idle if it's not.
|
|
|
|
// More info: https://github.com/zeit/next.js/pull/1511
|
|
|
|
if (module.hot && module.hot.status() !== 'idle') {
|
|
|
|
console.log(`Waiting for webpack to become "idle" to initialize the page: "${route}"`)
|
|
|
|
|
|
|
|
const check = (status) => {
|
|
|
|
if (status === 'idle') {
|
|
|
|
module.hot.removeStatusHandler(check)
|
|
|
|
register()
|
|
|
|
}
|
2017-04-06 06:49:00 +00:00
|
|
|
}
|
2018-12-31 18:06:36 +00:00
|
|
|
module.hot.status(check)
|
|
|
|
return
|
2017-04-06 06:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 18:06:36 +00:00
|
|
|
|
|
|
|
register()
|
2017-04-03 18:10:24 +00:00
|
|
|
}
|
2017-04-04 19:55:56 +00:00
|
|
|
|
2018-11-24 23:47:39 +00:00
|
|
|
async prefetch (route) {
|
|
|
|
route = this.normalizeRoute(route)
|
|
|
|
const scriptRoute = route === '/' ? '/index.js' : `${route}.js`
|
|
|
|
if (this.prefetchCache.has(scriptRoute)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.prefetchCache.add(scriptRoute)
|
|
|
|
|
2018-12-13 18:08:23 +00:00
|
|
|
// Inspired by quicklink, license: https://github.com/GoogleChromeLabs/quicklink/blob/master/LICENSE
|
|
|
|
// Don't prefetch if the user is on 2G / Don't prefetch if Save-Data is enabled
|
|
|
|
if ('connection' in navigator) {
|
|
|
|
if ((navigator.connection.effectiveType || '').indexOf('2g') !== -1 || navigator.connection.saveData) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:58:40 +00:00
|
|
|
// Feature detection is used to see if preload is supported
|
|
|
|
// If not fall back to loading script tags before the page is loaded
|
2018-11-24 23:47:39 +00:00
|
|
|
// https://caniuse.com/#feat=link-rel-preload
|
2018-11-26 22:58:40 +00:00
|
|
|
if (hasPreload) {
|
|
|
|
const link = document.createElement('link')
|
|
|
|
link.rel = 'preload'
|
2018-12-13 00:05:21 +00:00
|
|
|
link.crossOrigin = process.crossOrigin
|
2018-11-26 22:58:40 +00:00
|
|
|
link.href = `${this.assetPrefix}/_next/static/${encodeURIComponent(this.buildId)}/pages${scriptRoute}`
|
|
|
|
link.as = 'script'
|
|
|
|
document.head.appendChild(link)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
await this.loadPage(route)
|
|
|
|
} else {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
window.addEventListener('load', () => {
|
|
|
|
this.loadPage(route).then(() => resolve(), reject)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-11-24 23:47:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-04 19:55:56 +00:00
|
|
|
clearCache (route) {
|
|
|
|
route = this.normalizeRoute(route)
|
|
|
|
delete this.pageCache[route]
|
|
|
|
delete this.loadingRoutes[route]
|
2017-05-09 07:42:48 +00:00
|
|
|
|
|
|
|
const script = document.getElementById(`__NEXT_PAGE__${route}`)
|
|
|
|
if (script) {
|
|
|
|
script.parentNode.removeChild(script)
|
|
|
|
}
|
2017-04-04 19:55:56 +00:00
|
|
|
}
|
2017-04-03 18:10:24 +00:00
|
|
|
}
|