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
16e4f9e7
Unverified
Commit
16e4f9e7
authored
Aug 13, 2020
by
Joe Haddad
Committed by
GitHub
Aug 13, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Strongly type Head Manager (#16144)
parent
9e65c6a9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
20 deletions
+24
-20
head-manager.ts
packages/next/client/head-manager.ts
+24
-20
No files found.
packages/next/client/head-manager.
j
s
→
packages/next/client/head-manager.
t
s
View file @
16e4f9e7
const
DOMAttributeNames
=
{
const
DOMAttributeNames
:
Record
<
string
,
string
>
=
{
acceptCharset
:
'accept-charset'
,
acceptCharset
:
'accept-charset'
,
className
:
'class'
,
className
:
'class'
,
htmlFor
:
'for'
,
htmlFor
:
'for'
,
httpEquiv
:
'http-equiv'
,
httpEquiv
:
'http-equiv'
,
}
}
function
reactElementToDOM
({
type
,
props
}
)
{
function
reactElementToDOM
({
type
,
props
}
:
JSX
.
Element
):
HTMLElement
{
const
el
=
document
.
createElement
(
type
)
const
el
=
document
.
createElement
(
type
)
for
(
const
p
in
props
)
{
for
(
const
p
in
props
)
{
if
(
!
props
.
hasOwnProperty
(
p
))
continue
if
(
!
props
.
hasOwnProperty
(
p
))
continue
...
@@ -27,9 +27,11 @@ function reactElementToDOM({ type, props }) {
...
@@ -27,9 +27,11 @@ function reactElementToDOM({ type, props }) {
return
el
return
el
}
}
function
updateElements
(
type
,
components
)
{
function
updateElements
(
type
:
string
,
components
:
JSX
.
Element
[]
)
{
const
headEl
=
document
.
getElementsByTagName
(
'head'
)[
0
]
const
headEl
=
document
.
getElementsByTagName
(
'head'
)[
0
]
const
headCountEl
=
headEl
.
querySelector
(
'meta[name=next-head-count]'
)
const
headCountEl
:
HTMLMetaElement
=
headEl
.
querySelector
(
'meta[name=next-head-count]'
)
as
HTMLMetaElement
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
!
headCountEl
)
{
if
(
!
headCountEl
)
{
console
.
error
(
console
.
error
(
...
@@ -40,44 +42,46 @@ function updateElements(type, components) {
...
@@ -40,44 +42,46 @@ function updateElements(type, components) {
}
}
const
headCount
=
Number
(
headCountEl
.
content
)
const
headCount
=
Number
(
headCountEl
.
content
)
const
oldTags
=
[]
const
oldTags
:
Element
[]
=
[]
for
(
for
(
let
i
=
0
,
j
=
headCountEl
.
previousElementSibling
;
let
i
=
0
,
j
=
headCountEl
.
previousElementSibling
;
i
<
headCount
;
i
<
headCount
;
i
++
,
j
=
j
.
previousElementSibling
i
++
,
j
=
j
!
.
previousElementSibling
)
{
)
{
if
(
j
.
tagName
.
toLowerCase
()
===
type
)
{
if
(
j
!
.
tagName
.
toLowerCase
()
===
type
)
{
oldTags
.
push
(
j
)
oldTags
.
push
(
j
!
)
}
}
}
}
const
newTags
=
components
.
map
(
reactElementToDOM
).
filter
((
newTag
)
=>
{
const
newTags
=
(
components
.
map
(
reactElementToDOM
)
as
HTMLElement
[]).
filter
(
for
(
let
k
=
0
,
len
=
oldTags
.
length
;
k
<
len
;
k
++
)
{
(
newTag
)
=>
{
const
oldTag
=
oldTags
[
k
]
for
(
let
k
=
0
,
len
=
oldTags
.
length
;
k
<
len
;
k
++
)
{
if
(
oldTag
.
isEqualNode
(
newTag
))
{
const
oldTag
=
oldTags
[
k
]
oldTags
.
splice
(
k
,
1
)
if
(
oldTag
.
isEqualNode
(
newTag
))
{
return
false
oldTags
.
splice
(
k
,
1
)
return
false
}
}
}
return
true
}
}
return
true
)
})
oldTags
.
forEach
((
t
)
=>
t
.
parentNode
.
removeChild
(
t
))
oldTags
.
forEach
((
t
)
=>
t
.
parentNode
!
.
removeChild
(
t
))
newTags
.
forEach
((
t
)
=>
headEl
.
insertBefore
(
t
,
headCountEl
))
newTags
.
forEach
((
t
)
=>
headEl
.
insertBefore
(
t
,
headCountEl
))
headCountEl
.
content
=
(
headCount
-
oldTags
.
length
+
newTags
.
length
).
toString
()
headCountEl
.
content
=
(
headCount
-
oldTags
.
length
+
newTags
.
length
).
toString
()
}
}
export
default
function
initHeadManager
()
{
export
default
function
initHeadManager
()
{
let
updatePromise
=
null
let
updatePromise
:
Promise
<
void
>
|
null
=
null
return
{
return
{
mountedInstances
:
new
Set
(),
mountedInstances
:
new
Set
(),
updateHead
:
(
head
)
=>
{
updateHead
:
(
head
:
JSX
.
Element
[]
)
=>
{
const
promise
=
(
updatePromise
=
Promise
.
resolve
().
then
(()
=>
{
const
promise
=
(
updatePromise
=
Promise
.
resolve
().
then
(()
=>
{
if
(
promise
!==
updatePromise
)
return
if
(
promise
!==
updatePromise
)
return
updatePromise
=
null
updatePromise
=
null
const
tags
=
{}
const
tags
:
Record
<
string
,
JSX
.
Element
[]
>
=
{}
head
.
forEach
((
h
)
=>
{
head
.
forEach
((
h
)
=>
{
const
components
=
tags
[
h
.
type
]
||
[]
const
components
=
tags
[
h
.
type
]
||
[]
...
...
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