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
3c38722a
Unverified
Commit
3c38722a
authored
Jul 16, 2025
by
Jiachi Liu
Committed by
GitHub
Jul 16, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[metadata] replace for initial body icon case (#81688)
parent
7bcef687
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
16 deletions
+90
-16
node-web-streams-helper.ts
...s/next/src/server/stream-utils/node-web-streams-helper.ts
+31
-13
page.tsx
...p-dir/metadata-icons/app/custom-icon/delay-icons/page.tsx
+28
-0
metadata-icons.test.ts
test/e2e/app-dir/metadata-icons/metadata-icons.test.ts
+31
-3
No files found.
packages/next/src/server/stream-utils/node-web-streams-helper.ts
View file @
3c38722a
...
@@ -263,21 +263,39 @@ function createMetadataTransformStream(
...
@@ -263,21 +263,39 @@ function createMetadataTransformStream(
// Check if icon mark is inside <head> tag in the first chunk.
// Check if icon mark is inside <head> tag in the first chunk.
if
(
chunkIndex
===
0
)
{
if
(
chunkIndex
===
0
)
{
closedHeadIndex
=
indexOfUint8Array
(
chunk
,
ENCODED_TAGS
.
CLOSED
.
HEAD
)
closedHeadIndex
=
indexOfUint8Array
(
chunk
,
ENCODED_TAGS
.
CLOSED
.
HEAD
)
// The mark icon is located in the 1st chunk before the head tag.
if
(
iconMarkIndex
!==
-
1
)
{
// We do not need to insert the script tag in this case because it's in the head.
// The mark icon is located in the 1st chunk before the head tag.
// Just remove the icon mark from the chunk.
// We do not need to insert the script tag in this case because it's in the head.
if
(
iconMarkIndex
<
closedHeadIndex
&&
iconMarkIndex
!==
-
1
)
{
// Just remove the icon mark from the chunk.
const
replaced
=
new
Uint8Array
(
chunk
.
length
-
iconMarkLength
)
if
(
iconMarkIndex
<
closedHeadIndex
)
{
const
replaced
=
new
Uint8Array
(
chunk
.
length
-
iconMarkLength
)
// Remove the icon mark from the chunk.
replaced
.
set
(
chunk
.
subarray
(
0
,
iconMarkIndex
))
// Remove the icon mark from the chunk.
replaced
.
set
(
replaced
.
set
(
chunk
.
subarray
(
0
,
iconMarkIndex
))
chunk
.
subarray
(
iconMarkIndex
+
iconMarkLength
),
replaced
.
set
(
iconMarkIndex
chunk
.
subarray
(
iconMarkIndex
+
iconMarkLength
),
)
iconMarkIndex
chunk
=
replaced
)
chunk
=
replaced
}
else
{
// The icon mark is after the head tag, replace and insert the script tag at that position.
const
insertion
=
await
insert
()
const
encodedInsertion
=
encoder
.
encode
(
insertion
)
const
insertionLength
=
encodedInsertion
.
length
const
replaced
=
new
Uint8Array
(
chunk
.
length
-
iconMarkLength
+
insertionLength
)
replaced
.
set
(
chunk
.
subarray
(
0
,
iconMarkIndex
))
replaced
.
set
(
encodedInsertion
,
iconMarkIndex
)
replaced
.
set
(
chunk
.
subarray
(
iconMarkIndex
+
iconMarkLength
),
iconMarkIndex
+
insertionLength
)
chunk
=
replaced
}
isMarkRemoved
=
true
isMarkRemoved
=
true
}
}
// If there's no icon mark located, it will be handled later when if present in the following chunks.
}
else
{
}
else
{
// When it's appeared in the following chunks, we'll need to
// When it's appeared in the following chunks, we'll need to
// remove the mark and then insert the script tag at that position.
// remove the mark and then insert the script tag at that position.
...
...
test/e2e/app-dir/metadata-icons/app/custom-icon/delay-icons/page.tsx
0 → 100644
View file @
3c38722a
import
Link
from
'next/link'
import
{
connection
}
from
'next/server'
export
default
async
function
Page
()
{
return
(
<
div
>
<
h1
>
Delay Icons
</
h1
>
<
Link
id=
"custom-icon-sub-link"
href=
"/custom-icon/sub"
>
Go to another page with custom icon
</
Link
>
<
br
/>
<
Link
id=
"custom-icon-sub-link"
href=
"/custom-icon"
>
Go to root page with custom icon
</
Link
>
</
div
>
)
}
export
async
function
generateMetadata
()
{
await
connection
()
return
{
// This long text description will lead to the metadata being inserted after the head tag.
description
:
'long text description'
.
repeat
(
1000
),
icons
:
{
icon
:
`/heart.png`
,
},
}
}
test/e2e/app-dir/metadata-icons/metadata-icons.test.ts
View file @
3c38722a
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
retry
}
from
'next-test-utils'
import
{
retry
}
from
'next-test-utils'
const
iconInsertionScript
=
`document.querySelectorAll('body link[rel="icon"], body link[rel="apple-touch-icon"]').forEach(el => document.head.appendChild(el))`
describe
(
'app-dir - metadata-icons'
,
()
=>
{
describe
(
'app-dir - metadata-icons'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
const
{
next
}
=
nextTestSetup
({
files
:
__dirname
,
files
:
__dirname
,
...
@@ -45,15 +47,13 @@ describe('app-dir - metadata-icons', () => {
...
@@ -45,15 +47,13 @@ describe('app-dir - metadata-icons', () => {
})
})
it
(
'should not contain icon insertion script when metadata is rendered in head'
,
async
()
=>
{
it
(
'should not contain icon insertion script when metadata is rendered in head'
,
async
()
=>
{
const
iconInsertionScript
=
`document.querySelectorAll('body link[rel="icon"], body link[rel="apple-touch-icon"]').forEach(el => document.head.appendChild(el))`
const
suspendedHtml
=
await
next
.
render
(
'/custom-icon'
)
const
suspendedHtml
=
await
next
.
render
(
'/custom-icon'
)
expect
(
suspendedHtml
).
toContain
(
iconInsertionScript
)
expect
(
suspendedHtml
).
toContain
(
iconInsertionScript
)
})
})
it
(
'should not contain icon replacement mark in html or after hydration'
,
async
()
=>
{
it
(
'should not contain icon replacement mark in html or after hydration'
,
async
()
=>
{
const
html
=
await
next
.
render
(
'/custom-icon'
)
const
html
=
await
next
.
render
(
'/custom-icon'
)
expect
(
html
).
not
.
toContain
(
'<meta name="«nxt-icon»"
content=""/
>'
)
expect
(
html
).
not
.
toContain
(
'<meta name="«nxt-icon»">'
)
expect
(
html
).
not
.
toContain
(
'«nxt-icon»'
)
expect
(
html
).
not
.
toContain
(
'«nxt-icon»'
)
const
browser
=
await
next
.
browser
(
'/custom-icon'
)
const
browser
=
await
next
.
browser
(
'/custom-icon'
)
...
@@ -112,4 +112,32 @@ describe('app-dir - metadata-icons', () => {
...
@@ -112,4 +112,32 @@ describe('app-dir - metadata-icons', () => {
])
])
})
})
})
})
it
(
'should re-insert the icons into head when icons are inserted in body during initial chunk'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/custom-icon/delay-icons'
)
expect
(
$
(
'meta[name="«nxt-icon»"]'
).
length
).
toBe
(
0
)
// body will contain the icons and the script to insert them into head
const
body
=
$
(
'body'
)
const
icons
=
body
.
find
(
'link[rel="icon"]'
)
expect
(
icons
.
length
).
toBe
(
2
)
expect
(
Array
.
from
(
icons
.
map
((
_
,
el
)
=>
$
(
el
).
attr
(
'href'
)))).
toContain
(
'/heart.png'
)
const
bodyHtml
=
body
.
html
()
expect
(
bodyHtml
).
toContain
(
iconInsertionScript
)
// icons should be inserted into head after hydration
const
browser
=
await
next
.
browser
(
'/custom-icon/delay-icons'
)
await
retry
(
async
()
=>
{
const
iconsInHead
=
await
browser
.
elementsByCss
(
'head link[rel="icon"]'
)
const
iconUrls
=
await
Promise
.
all
(
iconsInHead
.
map
(
async
(
el
)
=>
(
await
el
.
getAttribute
(
'href'
)).
split
(
'?'
)[
0
]
)
)
expect
(
iconUrls
).
toEqual
([
'/favicon.ico'
,
'/heart.png'
])
})
})
})
})
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