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
edbb32ca
Unverified
Commit
edbb32ca
authored
Jul 28, 2020
by
Arsalan Khattak
Committed by
GitHub
Jul 28, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Next.js prefetching should use requestIdleCallback (#14580)
parent
fb81ecb2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
11 deletions
+18
-11
page-loader.js
packages/next/client/page-loader.js
+18
-11
No files found.
packages/next/client/page-loader.js
View file @
edbb32ca
...
@@ -31,6 +31,12 @@ const relPrefetch =
...
@@ -31,6 +31,12 @@ const relPrefetch =
const
hasNoModule
=
'noModule'
in
document
.
createElement
(
'script'
)
const
hasNoModule
=
'noModule'
in
document
.
createElement
(
'script'
)
const
requestIdleCallback
=
window
.
requestIdleCallback
||
function
(
cb
)
{
return
setTimeout
(
cb
,
1
)
}
/** @param {string} route */
/** @param {string} route */
function
normalizeRoute
(
route
)
{
function
normalizeRoute
(
route
)
{
if
(
route
[
0
]
!==
'/'
)
{
if
(
route
[
0
]
!==
'/'
)
{
...
@@ -184,8 +190,8 @@ export default class PageLoader {
...
@@ -184,8 +190,8 @@ export default class PageLoader {
prefetchData
(
href
,
asPath
)
{
prefetchData
(
href
,
asPath
)
{
const
{
pathname
:
hrefPathname
}
=
parseRelativeUrl
(
href
)
const
{
pathname
:
hrefPathname
}
=
parseRelativeUrl
(
href
)
const
route
=
normalizeRoute
(
hrefPathname
)
const
route
=
normalizeRoute
(
hrefPathname
)
return
this
.
promisedSsgManifest
.
then
(
return
this
.
promisedSsgManifest
.
then
(
(
s
,
_dataHref
)
=>
{
(
s
,
_dataHref
)
=>
requestIdleCallback
(()
=>
{
// Check if the route requires a data file
// Check if the route requires a data file
s
.
has
(
route
)
&&
s
.
has
(
route
)
&&
// Try to generate data href, noop when falsy
// Try to generate data href, noop when falsy
...
@@ -196,7 +202,8 @@ export default class PageLoader {
...
@@ -196,7 +202,8 @@ export default class PageLoader {
)
&&
)
&&
// Inject the `<link rel=prefetch>` tag for above computed `href`.
// Inject the `<link rel=prefetch>` tag for above computed `href`.
appendLink
(
_dataHref
,
relPrefetch
,
'fetch'
)
appendLink
(
_dataHref
,
relPrefetch
,
'fetch'
)
)
})
})
}
}
loadPage
(
route
)
{
loadPage
(
route
)
{
...
...
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