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
8a74f0ae
Unverified
Commit
8a74f0ae
authored
Jul 31, 2019
by
Joe Haddad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Golf page-loader (#8190)
parent
00b56c6e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
33 deletions
+22
-33
page-loader.js
packages/next/client/page-loader.js
+22
-33
No files found.
packages/next/client/page-loader.js
View file @
8a74f0ae
...
...
@@ -2,19 +2,24 @@
import
mitt
from
'next-server/dist/lib/mitt'
import
unfetch
from
'unfetch'
// smaller version of https://gist.github.com/igrigorik/a02f2359f3bc50ca7a9c
function
supportsPreload
(
list
)
{
if
(
!
list
||
!
list
.
supports
)
{
return
false
}
function
supportsPreload
(
el
)
{
try
{
return
l
ist
.
supports
(
'preload'
)
}
catch
(
e
)
{
return
el
.
relL
ist
.
supports
(
'preload'
)
}
catch
{
return
false
}
}
const
hasPreload
=
supportsPreload
(
document
.
createElement
(
'link'
).
relList
)
const
hasPreload
=
supportsPreload
(
document
.
createElement
(
'link'
))
function
preloadScript
(
url
)
{
const
link
=
document
.
createElement
(
'link'
)
link
.
rel
=
'preload'
link
.
crossOrigin
=
process
.
crossOrigin
link
.
href
=
url
link
.
as
=
'script'
document
.
head
.
appendChild
(
link
)
}
export
default
class
PageLoader
{
constructor
(
buildId
,
assetPrefix
)
{
...
...
@@ -189,12 +194,10 @@ export default class PageLoader {
this
.
prefetchCache
.
add
(
scriptRoute
)
// 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
)
{
let
cn
if
((
cn
=
navigator
.
connection
))
{
// Don't prefetch if the user is on 2G or if Save-Data is enabled.
if
((
cn
.
effectiveType
||
''
).
indexOf
(
'2g'
)
!==
-
1
||
cn
.
saveData
)
{
return
}
}
...
...
@@ -205,14 +208,11 @@ export default class PageLoader {
if
(
hasPreload
)
{
await
this
.
promisedBuildId
const
link
=
document
.
createElement
(
'link'
)
link
.
rel
=
'preload'
link
.
crossOrigin
=
process
.
crossOrigin
link
.
href
=
`
${
this
.
assetPrefix
}
/_next/static/
${
encodeURIComponent
(
this
.
buildId
)}
/pages
${
scriptRoute
}
`
link
.
as
=
'script'
document
.
head
.
appendChild
(
link
)
preloadScript
(
`
${
this
.
assetPrefix
}
/_next/static/
${
encodeURIComponent
(
this
.
buildId
)}
/pages
${
scriptRoute
}
`
)
return
}
...
...
@@ -226,15 +226,4 @@ export default class PageLoader {
})
}
}
clearCache
(
route
)
{
route
=
this
.
normalizeRoute
(
route
)
delete
this
.
pageCache
[
route
]
delete
this
.
loadingRoutes
[
route
]
const
script
=
document
.
getElementById
(
`__NEXT_PAGE__
${
route
}
`
)
if
(
script
)
{
script
.
parentNode
.
removeChild
(
script
)
}
}
}
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