Commit 8e4e2e3e authored by Naoyuki Kanezawa's avatar Naoyuki Kanezawa Committed by Arunoda Susiripala

improve prefetch (#1161)

parent 00e7a297
// based on https://github.com/sindresorhus/p-queue (MIT)
// modified for browser support
class Queue {
constructor () {
this._queue = []
}
enqueue (run) {
this._queue.push(run)
}
dequeue () {
return this._queue.shift()
}
get size () {
return this._queue.length
}
}
export default class PQueue {
constructor (opts) {
opts = Object.assign({
concurrency: Infinity,
queueClass: Queue
}, opts)
if (opts.concurrency < 1) {
throw new TypeError('Expected `concurrency` to be a number from 1 and up')
}
this.queue = new opts.queueClass() // eslint-disable-line new-cap
this._pendingCount = 0
this._concurrency = opts.concurrency
this._resolveEmpty = () => {}
}
_next () {
this._pendingCount--
if (this.queue.size > 0) {
this.queue.dequeue()()
} else {
this._resolveEmpty()
}
}
add (fn, opts) {
return new Promise((resolve, reject) => {
const run = () => {
this._pendingCount++
fn().then(
val => {
resolve(val)
this._next()
},
err => {
reject(err)
this._next()
}
)
}
if (this._pendingCount < this._concurrency) {
run()
} else {
this.queue.enqueue(run, opts)
}
})
}
onEmpty () {
return new Promise(resolve => {
const existingResolve = this._resolveEmpty
this._resolveEmpty = () => {
existingResolve()
resolve()
}
})
}
get size () {
return this.queue.size
}
get pending () {
return this._pendingCount
}
}
/* global __NEXT_DATA__, fetch */ /* global fetch */
import { parse, format } from 'url' import { parse, format } from 'url'
import { EventEmitter } from 'events'
import evalScript from '../eval-script' import evalScript from '../eval-script'
import shallowEquals from '../shallow-equals' import shallowEquals from '../shallow-equals'
import { EventEmitter } from 'events' import PQueue from '../p-queue'
import { loadGetInitialProps, LockManager, getLocationOrigin } from '../utils' import { loadGetInitialProps, getLocationOrigin } from '../utils'
// Add "fetch" polyfill for older browsers // Add "fetch" polyfill for older browsers
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
...@@ -19,16 +20,15 @@ export default class Router extends EventEmitter { ...@@ -19,16 +20,15 @@ export default class Router extends EventEmitter {
// set up the component cache (by route keys) // set up the component cache (by route keys)
this.components = { [this.route]: { Component, err } } this.components = { [this.route]: { Component, err } }
// contain a map of response of prefetched routes
this.prefetchedRoutes = {}
this.prefetchingLockManager = new LockManager(2)
this.prefetchingRoutes = {}
// contain a map of promise of fetch routes
this.fetchingRoutes = {}
this.prefetchQueue = new PQueue({ concurrency: 2 })
this.ErrorComponent = ErrorComponent this.ErrorComponent = ErrorComponent
this.pathname = pathname this.pathname = pathname
this.query = query this.query = query
this.subscriptions = new Set() this.subscriptions = new Set()
this.componentLoadCancel = null this.componentLoadCancel = null
this.onPopState = this.onPopState.bind(this) this.onPopState = this.onPopState.bind(this)
...@@ -104,8 +104,7 @@ export default class Router extends EventEmitter { ...@@ -104,8 +104,7 @@ export default class Router extends EventEmitter {
async reload (route) { async reload (route) {
delete this.components[route] delete this.components[route]
delete this.prefetchedRoutes[route] delete this.fetchingRoutes[route]
this.prefetchingRoutes[route] = 'IGNORE'
if (route !== this.route) return if (route !== this.route) return
...@@ -227,37 +226,7 @@ export default class Router extends EventEmitter { ...@@ -227,37 +226,7 @@ export default class Router extends EventEmitter {
async prefetch (url) { async prefetch (url) {
const { pathname } = parse(url) const { pathname } = parse(url)
const route = toRoute(pathname) const route = toRoute(pathname)
return this.prefetchQueue.add(() => this.fetchRoute(route))
const done = await this.prefetchingLockManager.get()
// It's possible for some other "prefetch" process
// to start prefetching the same route
// So, we should not fetch it again
if (this.prefetchingRoutes[route]) return done()
// It's possible that, this is already prefetched.
// So, we should not fetch it again
if (this.prefetchedRoutes[route]) return done()
// Mark as we are prefetching the route
this.prefetchingRoutes[route] = true
const complete = () => {
delete this.prefetchingRoutes[route]
done()
}
try {
const res = await this.fetchUrl(route)
// Router.relaod() process may ask us to ignore the current prefetching route
// In that case, we need to discard it
if (this.prefetchingRoutes[route] !== 'IGNORE') {
this.prefetchedRoutes[route] = res
}
complete()
} catch (ex) {
complete()
throw ex
}
} }
async fetchComponent (route) { async fetchComponent (route) {
...@@ -269,11 +238,7 @@ export default class Router extends EventEmitter { ...@@ -269,11 +238,7 @@ export default class Router extends EventEmitter {
cancelled = true cancelled = true
} }
let jsonPageRes = this.prefetchedRoutes[route] const jsonPageRes = await this.fetchRoute(route)
if (!jsonPageRes) {
jsonPageRes = await this.fetchUrl(route)
}
const jsonData = await jsonPageRes.json() const jsonData = await jsonPageRes.json()
const newData = { const newData = {
...loadComponent(jsonData), ...loadComponent(jsonData),
...@@ -314,14 +279,21 @@ export default class Router extends EventEmitter { ...@@ -314,14 +279,21 @@ export default class Router extends EventEmitter {
return props return props
} }
async fetchUrl (route) { fetchRoute (route) {
const url = `/_next/${__NEXT_DATA__.buildId}/pages${route}` let promise = this.fetchingRoutes[route]
const res = await fetch(url, { if (!promise) {
promise = this.fetchingRoutes[route] = this.doFetchRoute(route)
}
return promise
}
doFetchRoute (route) {
const { buildId } = window.__NEXT_DATA__
const url = `/_next/${encodeURIComponent(buildId)}/pages${route}`
return fetch(url, {
method: 'GET', method: 'GET',
headers: { 'Accept': 'application/json' } headers: { 'Accept': 'application/json' }
}) })
return res
} }
abortComponentLoad () { abortComponentLoad () {
......
...@@ -54,34 +54,6 @@ export async function loadGetInitialProps (Component, ctx) { ...@@ -54,34 +54,6 @@ export async function loadGetInitialProps (Component, ctx) {
return props return props
} }
export class LockManager {
constructor (maxLocks) {
this.maxLocks = maxLocks
this.clients = []
this.runningLocks = 0
}
get () {
return new Promise((resolve) => {
this.clients.push(resolve)
this._giveLocksIfPossible()
})
}
_giveLocksIfPossible () {
if (this.runningLocks < this.maxLocks) {
const client = this.clients.shift()
if (!client) return
this.runningLocks ++
client(() => {
this.runningLocks --
this._giveLocksIfPossible()
})
}
}
}
export function getLocationOrigin () { export function getLocationOrigin () {
const { protocol, hostname, port } = window.location const { protocol, hostname, port } = window.location
return `${protocol}//${hostname}${port ? ':' + port : ''}` return `${protocol}//${hostname}${port ? ':' + port : ''}`
......
...@@ -5,55 +5,30 @@ describe('Router', () => { ...@@ -5,55 +5,30 @@ describe('Router', () => {
describe('.prefetch()', () => { describe('.prefetch()', () => {
it('should prefetch a given page', async () => { it('should prefetch a given page', async () => {
const router = new Router('/', {}) const router = new Router('/', {})
const res = { aa: 'res' } const promise = Promise.resolve()
const route = 'routex' const route = 'routex'
router.fetchUrl = (r) => { router.doFetchRoute = (r) => {
expect(r).toBe(route) expect(r).toBe(route)
return Promise.resolve(res) return promise
} }
await router.prefetch(route) await router.prefetch(route)
expect(router.prefetchedRoutes[route]).toBe(res) expect(router.fetchingRoutes[route]).toBe(promise)
}) })
it('should stop if it\'s prefetched already', async () => { it('should stop if it\'s prefetching already', async () => {
const router = new Router('/', {}) const router = new Router('/', {})
const route = 'routex' const route = 'routex'
router.prefetchedRoutes[route] = { aa: 'res' } router.fetchingRoutes[route] = Promise.resolve()
router.fetchUrl = () => { throw new Error('Should not happen') } router.doFetchRoute = () => { throw new Error('Should not happen') }
await router.prefetch(route) await router.prefetch(route)
}) })
it('should stop if it\'s currently prefetching', async () => {
const router = new Router('/', {})
const route = 'routex'
router.prefetchingRoutes[route] = true
router.fetchUrl = () => { throw new Error('Should not happen') }
await router.prefetch(route)
})
it('should ignore the response if it asked to do', async () => {
const router = new Router('/', {})
const res = { aa: 'res' }
const route = 'routex'
let called = false
router.fetchUrl = () => {
called = true
router.prefetchingRoutes[route] = 'IGNORE'
return Promise.resolve(res)
}
await router.prefetch(route)
expect(router.prefetchedRoutes[route]).toBeUndefined()
expect(called).toBe(true)
})
it('should only run two jobs at a time', async () => { it('should only run two jobs at a time', async () => {
const router = new Router('/', {}) const router = new Router('/', {})
let count = 0 let count = 0
router.fetchUrl = () => { router.doFetchRoute = () => {
count++ count++
return new Promise((resolve) => {}) return new Promise((resolve) => {})
} }
...@@ -66,22 +41,21 @@ describe('Router', () => { ...@@ -66,22 +41,21 @@ describe('Router', () => {
await new Promise((resolve) => setTimeout(resolve, 50)) await new Promise((resolve) => setTimeout(resolve, 50))
expect(count).toBe(2) expect(count).toBe(2)
expect(Object.keys(router.prefetchingRoutes)).toEqual(['route1', 'route2']) expect(Object.keys(router.fetchingRoutes)).toEqual(['route1', 'route2'])
}) })
it('should run all the jobs', async () => { it('should run all the jobs', async () => {
const router = new Router('/', {}) const router = new Router('/', {})
const routes = ['route1', 'route2', 'route3', 'route4'] const routes = ['route1', 'route2', 'route3', 'route4']
router.fetchUrl = () => Promise.resolve({ aa: 'res' }) router.doFetchRoute = () => Promise.resolve()
await router.prefetch(routes[0]) await router.prefetch(routes[0])
await router.prefetch(routes[1]) await router.prefetch(routes[1])
await router.prefetch(routes[2]) await router.prefetch(routes[2])
await router.prefetch(routes[3]) await router.prefetch(routes[3])
expect(Object.keys(router.prefetchedRoutes)).toEqual(routes) expect(Object.keys(router.fetchingRoutes)).toEqual(routes)
expect(Object.keys(router.prefetchingRoutes)).toEqual([])
}) })
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment