Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
8e4e2e3e
Commit
8e4e2e3e
authored
Feb 15, 2017
by
Naoyuki Kanezawa
Committed by
Arunoda Susiripala
Feb 15, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve prefetch (#1161)
parent
00e7a297
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
117 additions
and
116 deletions
+117
-116
p-queue.js
lib/p-queue.js
+83
-0
router.js
lib/router/router.js
+23
-51
utils.js
lib/utils.js
+0
-28
router.test.js
test/unit/router.test.js
+11
-37
No files found.
lib/p-queue.js
0 → 100644
View file @
8e4e2e3e
// 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
}
}
lib/router/router.js
View file @
8e4e2e3e
/* global
__NEXT_DATA__,
fetch */
/* global fetch */
import
{
parse
,
format
}
from
'url'
import
{
EventEmitter
}
from
'events'
import
evalScript
from
'../eval-script'
import
shallowEquals
from
'../shallow-equals'
import
{
EventEmitter
}
from
'events
'
import
{
loadGetInitialProps
,
LockManager
,
getLocationOrigin
}
from
'../utils'
import
PQueue
from
'../p-queue
'
import
{
loadGetInitialProps
,
getLocationOrigin
}
from
'../utils'
// Add "fetch" polyfill for older browsers
if
(
typeof
window
!==
'undefined'
)
{
...
...
@@ -19,16 +20,15 @@ export default class Router extends EventEmitter {
// set up the component cache (by route keys)
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
.
pathname
=
pathname
this
.
query
=
query
this
.
subscriptions
=
new
Set
()
this
.
componentLoadCancel
=
null
this
.
onPopState
=
this
.
onPopState
.
bind
(
this
)
...
...
@@ -104,8 +104,7 @@ export default class Router extends EventEmitter {
async
reload
(
route
)
{
delete
this
.
components
[
route
]
delete
this
.
prefetchedRoutes
[
route
]
this
.
prefetchingRoutes
[
route
]
=
'IGNORE'
delete
this
.
fetchingRoutes
[
route
]
if
(
route
!==
this
.
route
)
return
...
...
@@ -227,37 +226,7 @@ export default class Router extends EventEmitter {
async
prefetch
(
url
)
{
const
{
pathname
}
=
parse
(
url
)
const
route
=
toRoute
(
pathname
)
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
}
return
this
.
prefetchQueue
.
add
(()
=>
this
.
fetchRoute
(
route
))
}
async
fetchComponent
(
route
)
{
...
...
@@ -269,11 +238,7 @@ export default class Router extends EventEmitter {
cancelled
=
true
}
let
jsonPageRes
=
this
.
prefetchedRoutes
[
route
]
if
(
!
jsonPageRes
)
{
jsonPageRes
=
await
this
.
fetchUrl
(
route
)
}
const
jsonPageRes
=
await
this
.
fetchRoute
(
route
)
const
jsonData
=
await
jsonPageRes
.
json
()
const
newData
=
{
...
loadComponent
(
jsonData
),
...
...
@@ -314,14 +279,21 @@ export default class Router extends EventEmitter {
return
props
}
async
fetchUrl
(
route
)
{
const
url
=
`/_next/
${
__NEXT_DATA__
.
buildId
}
/pages
${
route
}
`
const
res
=
await
fetch
(
url
,
{
fetchRoute
(
route
)
{
let
promise
=
this
.
fetchingRoutes
[
route
]
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'
,
headers
:
{
'Accept'
:
'application/json'
}
})
return
res
}
abortComponentLoad
()
{
...
...
lib/utils.js
View file @
8e4e2e3e
...
...
@@ -54,34 +54,6 @@ export async function loadGetInitialProps (Component, ctx) {
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
()
{
const
{
protocol
,
hostname
,
port
}
=
window
.
location
return
`
${
protocol
}
//
${
hostname
}${
port
?
':'
+
port
:
''
}
`
...
...
test/unit/router.test.js
View file @
8e4e2e3e
...
...
@@ -5,55 +5,30 @@ describe('Router', () => {
describe
(
'.prefetch()'
,
()
=>
{
it
(
'should prefetch a given page'
,
async
()
=>
{
const
router
=
new
Router
(
'/'
,
{})
const
res
=
{
aa
:
'res'
}
const
promise
=
Promise
.
resolve
()
const
route
=
'routex'
router
.
fetchUrl
=
(
r
)
=>
{
router
.
doFetchRoute
=
(
r
)
=>
{
expect
(
r
).
toBe
(
route
)
return
Promise
.
resolve
(
res
)
return
promise
}
await
router
.
prefetch
(
route
)
expect
(
router
.
prefetchedRoutes
[
route
]).
toBe
(
res
)
expect
(
router
.
fetchingRoutes
[
route
]).
toBe
(
promise
)
})
it
(
'should stop if it
\'
s prefetch
ed
already'
,
async
()
=>
{
it
(
'should stop if it
\'
s prefetch
ing
already'
,
async
()
=>
{
const
router
=
new
Router
(
'/'
,
{})
const
route
=
'routex'
router
.
prefetchedRoutes
[
route
]
=
{
aa
:
'res'
}
router
.
fetchUrl
=
()
=>
{
throw
new
Error
(
'Should not happen'
)
}
router
.
fetchingRoutes
[
route
]
=
Promise
.
resolve
()
router
.
doFetchRoute
=
()
=>
{
throw
new
Error
(
'Should not happen'
)
}
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
()
=>
{
const
router
=
new
Router
(
'/'
,
{})
let
count
=
0
router
.
fetchUrl
=
()
=>
{
router
.
doFetchRoute
=
()
=>
{
count
++
return
new
Promise
((
resolve
)
=>
{})
}
...
...
@@ -66,22 +41,21 @@ describe('Router', () => {
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
50
))
expect
(
count
).
toBe
(
2
)
expect
(
Object
.
keys
(
router
.
pre
fetchingRoutes
)).
toEqual
([
'route1'
,
'route2'
])
expect
(
Object
.
keys
(
router
.
fetchingRoutes
)).
toEqual
([
'route1'
,
'route2'
])
})
it
(
'should run all the jobs'
,
async
()
=>
{
const
router
=
new
Router
(
'/'
,
{})
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
[
1
])
await
router
.
prefetch
(
routes
[
2
])
await
router
.
prefetch
(
routes
[
3
])
expect
(
Object
.
keys
(
router
.
prefetchedRoutes
)).
toEqual
(
routes
)
expect
(
Object
.
keys
(
router
.
prefetchingRoutes
)).
toEqual
([])
expect
(
Object
.
keys
(
router
.
fetchingRoutes
)).
toEqual
(
routes
)
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment