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
328abb72
Unverified
Commit
328abb72
authored
Aug 27, 2020
by
Joe Haddad
Committed by
GitHub
Aug 27, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not rely on `cssText` (#16611)
parent
42e8dcf2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
37 deletions
+41
-37
index.tsx
packages/next/client/index.tsx
+2
-21
page-loader.ts
packages/next/client/page-loader.ts
+39
-16
No files found.
packages/next/client/index.tsx
View file @
328abb72
...
@@ -17,7 +17,7 @@ import * as envConfig from '../next-server/lib/runtime-config'
...
@@ -17,7 +17,7 @@ import * as envConfig from '../next-server/lib/runtime-config'
import
{
getURL
,
loadGetInitialProps
,
ST
}
from
'../next-server/lib/utils'
import
{
getURL
,
loadGetInitialProps
,
ST
}
from
'../next-server/lib/utils'
import
type
{
NEXT_DATA
}
from
'../next-server/lib/utils'
import
type
{
NEXT_DATA
}
from
'../next-server/lib/utils'
import
initHeadManager
from
'./head-manager'
import
initHeadManager
from
'./head-manager'
import
PageLoader
,
{
StyleSheetTuple
}
from
'./page-loader'
import
PageLoader
,
{
looseToArray
,
StyleSheetTuple
}
from
'./page-loader'
import
measureWebVitals
from
'./performance-relayer'
import
measureWebVitals
from
'./performance-relayer'
import
{
createRouter
,
makePublicRouterInstance
}
from
'./router'
import
{
createRouter
,
makePublicRouterInstance
}
from
'./router'
...
@@ -84,26 +84,7 @@ if (hasBasePath(asPath)) {
...
@@ -84,26 +84,7 @@ if (hasBasePath(asPath)) {
type
RegisterFn
=
(
input
:
[
string
,
()
=>
void
])
=>
void
type
RegisterFn
=
(
input
:
[
string
,
()
=>
void
])
=>
void
const
looseToArray
=
<
T
extends
{}
>
(input: any): T[] =
>
[].slice.call(input)
const
pageLoader
=
new
PageLoader
(
buildId
,
prefix
,
page
)
const pageLoader = new PageLoader(
buildId,
prefix,
page,
looseToArray
<
CSSStyleSheet
>
(document.styleSheets)
.filter(
(el: CSSStyleSheet) =
>
el.ownerNode
&&
(el.ownerNode as Element).tagName === 'LINK'
&&
(el.ownerNode as Element).hasAttribute('data-n-p')
)
.map((sheet) =
>
(
{
href
:
(
sheet
.
ownerNode
as
Element
).
getAttribute
(
'href'
)
!
,
text
:
looseToArray
<
CSSRule
>
(
sheet
.
cssRules
)
.
map
((
r
)
=>
r
.
cssText
)
.
join
(
''
),
}
))
)
const
register
:
RegisterFn
=
([
r
,
f
])
=>
pageLoader
.
registerPage
(
r
,
f
)
const
register
:
RegisterFn
=
([
r
,
f
])
=>
pageLoader
.
registerPage
(
r
,
f
)
if
(
window
.
__NEXT_P
)
{
if
(
window
.
__NEXT_P
)
{
// Defer page registration for another tick. This will increase the overall
// Defer page registration for another tick. This will increase the overall
...
...
packages/next/client/page-loader.ts
View file @
328abb72
...
@@ -12,6 +12,25 @@ import { searchParamsToUrlQuery } from '../next-server/lib/router/utils/querystr
...
@@ -12,6 +12,25 @@ import { searchParamsToUrlQuery } from '../next-server/lib/router/utils/querystr
import
{
getRouteMatcher
}
from
'../next-server/lib/router/utils/route-matcher'
import
{
getRouteMatcher
}
from
'../next-server/lib/router/utils/route-matcher'
import
{
getRouteRegex
}
from
'../next-server/lib/router/utils/route-regex'
import
{
getRouteRegex
}
from
'../next-server/lib/router/utils/route-regex'
export
const
looseToArray
=
<
T
extends
{}
>
(
input
:
any
):
T
[]
=>
[].
slice
.
call
(
input
)
function
getInitialStylesheets
():
StyleSheetTuple
[]
{
return
looseToArray
<
CSSStyleSheet
>
(
document
.
styleSheets
)
.
filter
(
(
el
:
CSSStyleSheet
)
=>
el
.
ownerNode
&&
(
el
.
ownerNode
as
Element
).
tagName
===
'LINK'
&&
(
el
.
ownerNode
as
Element
).
hasAttribute
(
'data-n-p'
)
)
.
map
((
sheet
)
=>
({
href
:
(
sheet
.
ownerNode
as
Element
).
getAttribute
(
'href'
)
!
,
text
:
looseToArray
<
CSSRule
>
(
sheet
.
cssRules
)
.
map
((
r
)
=>
r
.
cssText
)
.
join
(
''
),
}))
}
function
hasRel
(
rel
:
string
,
link
?:
HTMLLinkElement
)
{
function
hasRel
(
rel
:
string
,
link
?:
HTMLLinkElement
)
{
try
{
try
{
link
=
document
.
createElement
(
'link'
)
link
=
document
.
createElement
(
'link'
)
...
@@ -99,7 +118,6 @@ export type PageCacheEntry = { error: any } | GoodPageCache
...
@@ -99,7 +118,6 @@ export type PageCacheEntry = { error: any } | GoodPageCache
export
default
class
PageLoader
{
export
default
class
PageLoader
{
private
initialPage
:
string
private
initialPage
:
string
private
initialStyleSheets
:
StyleSheetTuple
[]
private
buildId
:
string
private
buildId
:
string
private
assetPrefix
:
string
private
assetPrefix
:
string
private
pageCache
:
Record
<
string
,
PageCacheEntry
>
private
pageCache
:
Record
<
string
,
PageCacheEntry
>
...
@@ -109,14 +127,8 @@ export default class PageLoader {
...
@@ -109,14 +127,8 @@ export default class PageLoader {
private
promisedSsgManifest
?:
Promise
<
ClientSsgManifest
>
private
promisedSsgManifest
?:
Promise
<
ClientSsgManifest
>
private
promisedDevPagesManifest
?:
Promise
<
any
>
private
promisedDevPagesManifest
?:
Promise
<
any
>
constructor
(
constructor
(
buildId
:
string
,
assetPrefix
:
string
,
initialPage
:
string
)
{
buildId
:
string
,
assetPrefix
:
string
,
initialPage
:
string
,
initialStyleSheets
:
StyleSheetTuple
[]
)
{
this
.
initialPage
=
initialPage
this
.
initialPage
=
initialPage
this
.
initialStyleSheets
=
initialStyleSheets
this
.
buildId
=
buildId
this
.
buildId
=
buildId
this
.
assetPrefix
=
assetPrefix
this
.
assetPrefix
=
assetPrefix
...
@@ -422,23 +434,34 @@ export default class PageLoader {
...
@@ -422,23 +434,34 @@ export default class PageLoader {
})
})
}
}
const
isInitialLoad
=
route
===
this
.
initialPage
const
promisedDeps
:
Promise
<
StyleSheetTuple
[]
>
=
const
promisedDeps
:
Promise
<
StyleSheetTuple
[]
>
=
// Shared styles will already be on the page:
// Shared styles will already be on the page:
route
===
'/_app'
||
route
===
'/_app'
||
// We use `style-loader` in development:
// We use `style-loader` in development:
process
.
env
.
NODE_ENV
!==
'production'
process
.
env
.
NODE_ENV
!==
'production'
?
Promise
.
resolve
([])
?
Promise
.
resolve
([])
:
route
===
this
.
initialPage
?
Promise
.
resolve
(
this
.
initialStyleSheets
)
:
// Tests that this does not block hydration:
:
// Tests that this does not block hydration:
// test/integration/css-fixtures/hydrate-without-deps/
// test/integration/css-fixtures/hydrate-without-deps/
this
.
getDependencies
(
route
)
(
isInitialLoad
.
then
((
deps
)
=>
deps
.
filter
((
d
)
=>
d
.
endsWith
(
'.css'
)))
?
Promise
.
resolve
(
.
then
((
cssFiles
)
=>
looseToArray
<
HTMLLinkElement
>
(
// These files should've already been fetched by now, so this
document
.
querySelectorAll
(
'link[data-n-p]'
)
// should resolve pretty much instantly.
).
map
((
e
)
=>
e
.
getAttribute
(
'href'
)
!
)
Promise
.
all
(
cssFiles
.
map
((
d
)
=>
fetchStyleSheet
(
d
)))
)
:
this
.
getDependencies
(
route
).
then
((
deps
)
=>
deps
.
filter
((
d
)
=>
d
.
endsWith
(
'.css'
))
)
).
then
((
cssFiles
)
=>
// These files should've already been fetched by now, so this
// should resolve instantly.
Promise
.
all
(
cssFiles
.
map
((
d
)
=>
fetchStyleSheet
(
d
))).
catch
(
(
err
)
=>
{
if
(
isInitialLoad
)
return
getInitialStylesheets
()
throw
err
}
)
)
)
promisedDeps
.
then
(
promisedDeps
.
then
(
(
deps
)
=>
register
(
deps
),
(
deps
)
=>
register
(
deps
),
(
error
)
=>
{
(
error
)
=>
{
...
...
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