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
513a7d5b
Unverified
Commit
513a7d5b
authored
Feb 26, 2020
by
Tim Neutkens
Committed by
GitHub
Feb 26, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update head-manager to compress better (#10687)
* Update head-manager to compress better * Decrease size limit test
parent
a81913f1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
88 deletions
+74
-88
head-manager.js
packages/next/client/head-manager.js
+70
-84
index.js
packages/next/client/index.js
+3
-3
index.test.js
test/integration/size-limit/test/index.test.js
+1
-1
No files found.
packages/next/client/head-manager.js
View file @
513a7d5b
...
...
@@ -5,90 +5,6 @@ const DOMAttributeNames = {
httpEquiv
:
'http-equiv'
,
}
export
default
class
HeadManager
{
constructor
()
{
this
.
updatePromise
=
null
}
updateHead
=
head
=>
{
const
promise
=
(
this
.
updatePromise
=
Promise
.
resolve
().
then
(()
=>
{
if
(
promise
!==
this
.
updatePromise
)
return
this
.
updatePromise
=
null
this
.
doUpdateHead
(
head
)
}))
}
doUpdateHead
(
head
)
{
const
tags
=
{}
head
.
forEach
(
h
=>
{
const
components
=
tags
[
h
.
type
]
||
[]
components
.
push
(
h
)
tags
[
h
.
type
]
=
components
})
this
.
updateTitle
(
tags
.
title
?
tags
.
title
[
0
]
:
null
)
const
types
=
[
'meta'
,
'base'
,
'link'
,
'style'
,
'script'
]
types
.
forEach
(
type
=>
{
this
.
updateElements
(
type
,
tags
[
type
]
||
[])
})
}
updateTitle
(
component
)
{
let
title
=
''
if
(
component
)
{
const
{
children
}
=
component
.
props
title
=
typeof
children
===
'string'
?
children
:
children
.
join
(
''
)
}
if
(
title
!==
document
.
title
)
document
.
title
=
title
}
updateElements
(
type
,
components
)
{
const
headEl
=
document
.
getElementsByTagName
(
'head'
)[
0
]
const
headCountEl
=
headEl
.
querySelector
(
'meta[name=next-head-count]'
)
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
!
headCountEl
)
{
console
.
error
(
'Warning: next-head-count is missing. https://err.sh/next.js/next-head-count-missing'
)
return
}
}
const
headCount
=
Number
(
headCountEl
.
content
)
const
oldTags
=
[]
for
(
let
i
=
0
,
j
=
headCountEl
.
previousElementSibling
;
i
<
headCount
;
i
++
,
j
=
j
.
previousElementSibling
)
{
if
(
j
.
tagName
.
toLowerCase
()
===
type
)
{
oldTags
.
push
(
j
)
}
}
const
newTags
=
components
.
map
(
reactElementToDOM
).
filter
(
newTag
=>
{
for
(
let
k
=
0
,
len
=
oldTags
.
length
;
k
<
len
;
k
++
)
{
const
oldTag
=
oldTags
[
k
]
if
(
oldTag
.
isEqualNode
(
newTag
))
{
oldTags
.
splice
(
k
,
1
)
return
false
}
}
return
true
})
oldTags
.
forEach
(
t
=>
t
.
parentNode
.
removeChild
(
t
))
newTags
.
forEach
(
t
=>
headEl
.
insertBefore
(
t
,
headCountEl
))
headCountEl
.
content
=
(
headCount
-
oldTags
.
length
+
newTags
.
length
).
toString
()
}
}
function
reactElementToDOM
({
type
,
props
})
{
const
el
=
document
.
createElement
(
type
)
for
(
const
p
in
props
)
{
...
...
@@ -110,3 +26,73 @@ function reactElementToDOM({ type, props }) {
}
return
el
}
function
updateElements
(
type
,
components
)
{
const
headEl
=
document
.
getElementsByTagName
(
'head'
)[
0
]
const
headCountEl
=
headEl
.
querySelector
(
'meta[name=next-head-count]'
)
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
!
headCountEl
)
{
console
.
error
(
'Warning: next-head-count is missing. https://err.sh/next.js/next-head-count-missing'
)
return
}
}
const
headCount
=
Number
(
headCountEl
.
content
)
const
oldTags
=
[]
for
(
let
i
=
0
,
j
=
headCountEl
.
previousElementSibling
;
i
<
headCount
;
i
++
,
j
=
j
.
previousElementSibling
)
{
if
(
j
.
tagName
.
toLowerCase
()
===
type
)
{
oldTags
.
push
(
j
)
}
}
const
newTags
=
components
.
map
(
reactElementToDOM
).
filter
(
newTag
=>
{
for
(
let
k
=
0
,
len
=
oldTags
.
length
;
k
<
len
;
k
++
)
{
const
oldTag
=
oldTags
[
k
]
if
(
oldTag
.
isEqualNode
(
newTag
))
{
oldTags
.
splice
(
k
,
1
)
return
false
}
}
return
true
})
oldTags
.
forEach
(
t
=>
t
.
parentNode
.
removeChild
(
t
))
newTags
.
forEach
(
t
=>
headEl
.
insertBefore
(
t
,
headCountEl
))
headCountEl
.
content
=
(
headCount
-
oldTags
.
length
+
newTags
.
length
).
toString
()
}
export
default
function
initHeadManager
()
{
let
updatePromise
=
null
return
head
=>
{
const
promise
=
(
updatePromise
=
Promise
.
resolve
().
then
(()
=>
{
if
(
promise
!==
updatePromise
)
return
updatePromise
=
null
const
tags
=
{}
head
.
forEach
(
h
=>
{
const
components
=
tags
[
h
.
type
]
||
[]
components
.
push
(
h
)
tags
[
h
.
type
]
=
components
})
const
titleComponent
=
tags
.
title
?
tags
.
title
[
0
]
:
null
let
title
=
''
if
(
titleComponent
)
{
const
{
children
}
=
titleComponent
.
props
title
=
typeof
children
===
'string'
?
children
:
children
.
join
(
''
)
}
if
(
title
!==
document
.
title
)
document
.
title
=
title
;[
'meta'
,
'base'
,
'link'
,
'style'
,
'script'
].
forEach
(
type
=>
{
updateElements
(
type
,
tags
[
type
]
||
[])
})
}))
}
}
packages/next/client/index.js
View file @
513a7d5b
/* global location */
import
React
from
'react'
import
ReactDOM
from
'react-dom'
import
HeadManager
from
'./head-manager'
import
init
HeadManager
from
'./head-manager'
import
{
createRouter
,
makePublicRouterInstance
}
from
'next/router'
import
mitt
from
'../next-server/lib/mitt'
import
{
loadGetInitialProps
,
getURL
,
ST
}
from
'../next-server/lib/utils'
...
...
@@ -57,7 +57,7 @@ if (window.__NEXT_P) {
window
.
__NEXT_P
=
[]
window
.
__NEXT_P
.
push
=
register
const
headManager
=
new
HeadManager
()
const
updateHead
=
init
HeadManager
()
const
appElement
=
document
.
getElementById
(
'__next'
)
let
lastAppProps
...
...
@@ -397,7 +397,7 @@ function AppContainer({ children }) {
}
>
<
RouterContext
.
Provider
value
=
{
makePublicRouterInstance
(
router
)}
>
<
HeadManagerContext
.
Provider
value
=
{
headManager
.
updateHead
}
>
<
HeadManagerContext
.
Provider
value
=
{
updateHead
}
>
{
children
}
<
/HeadManagerContext.Provider
>
<
/RouterContext.Provider
>
...
...
test/integration/size-limit/test/index.test.js
View file @
513a7d5b
...
...
@@ -80,7 +80,7 @@ describe('Production response size', () => {
)
// These numbers are without gzip compression!
const
delta
=
responseSizesBytes
-
23
8
*
1024
const
delta
=
responseSizesBytes
-
23
7
*
1024
expect
(
delta
).
toBeLessThanOrEqual
(
1024
)
// don't increase size more than 1kb
expect
(
delta
).
toBeGreaterThanOrEqual
(
-
1024
)
// don't decrease size more than 1kb without updating target
})
...
...
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