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
9fc19048
Unverified
Commit
9fc19048
authored
Mar 30, 2022
by
Shu Ding
Committed by
GitHub
Mar 30, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure the app shell is rendered before rendering the document (#35732)
parent
5a5b6174
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
4 deletions
+64
-4
node-web-streams-helper.ts
packages/next/server/node-web-streams-helper.ts
+50
-0
render.tsx
packages/next/server/render.tsx
+14
-4
No files found.
packages/next/server/node-web-streams-helper.ts
View file @
9fc19048
...
@@ -233,6 +233,56 @@ export function createFlushEffectStream(
...
@@ -233,6 +233,56 @@ export function createFlushEffectStream(
})
})
}
}
export
async
function
renderToInitialStream
({
ReactDOMServer
,
element
,
}:
{
ReactDOMServer
:
typeof
import
(
'react-dom/server'
)
element
:
React
.
ReactElement
}):
Promise
<
ReadableStream
<
Uint8Array
>
&
{
allReady
?:
Promise
<
void
>
}
>
{
return
await
(
ReactDOMServer
as
any
).
renderToReadableStream
(
element
)
}
export
async
function
continueFromInitialStream
({
suffix
,
dataStream
,
generateStaticHTML
,
flushEffectHandler
,
renderStream
,
}:
{
suffix
?:
string
dataStream
?:
ReadableStream
<
Uint8Array
>
generateStaticHTML
:
boolean
flushEffectHandler
?:
()
=>
Promise
<
string
>
renderStream
:
ReadableStream
<
Uint8Array
>
&
{
allReady
?:
Promise
<
void
>
}
}):
Promise
<
ReadableStream
<
Uint8Array
>>
{
const
closeTag
=
'</body></html>'
const
suffixUnclosed
=
suffix
?
suffix
.
split
(
closeTag
)[
0
]
:
null
if
(
generateStaticHTML
)
{
await
renderStream
.
allReady
}
const
transforms
:
Array
<
TransformStream
<
Uint8Array
,
Uint8Array
>>
=
[
createBufferedTransformStream
(),
flushEffectHandler
?
createFlushEffectStream
(
flushEffectHandler
)
:
null
,
suffixUnclosed
!=
null
?
createPrefixStream
(
suffixUnclosed
)
:
null
,
dataStream
?
createInlineDataStream
(
dataStream
)
:
null
,
suffixUnclosed
!=
null
?
createSuffixStream
(
closeTag
)
:
null
,
].
filter
(
Boolean
)
as
any
return
transforms
.
reduce
(
(
readable
,
transform
)
=>
pipeThrough
(
readable
,
transform
),
renderStream
)
}
export
async
function
renderToStream
({
export
async
function
renderToStream
({
ReactDOMServer
,
ReactDOMServer
,
element
,
element
,
...
...
packages/next/server/render.tsx
View file @
9fc19048
...
@@ -74,6 +74,8 @@ import {
...
@@ -74,6 +74,8 @@ import {
chainStreams
,
chainStreams
,
createBufferedTransformStream
,
createBufferedTransformStream
,
renderToStream
,
renderToStream
,
renderToInitialStream
,
continueFromInitialStream
,
}
from
'./node-web-streams-helper'
}
from
'./node-web-streams-helper'
import
{
ImageConfigContext
}
from
'../shared/lib/image-config-context'
import
{
ImageConfigContext
}
from
'../shared/lib/image-config-context'
import
{
FlushEffectsContext
}
from
'../shared/lib/flush-effects'
import
{
FlushEffectsContext
}
from
'../shared/lib/flush-effects'
...
@@ -1354,11 +1356,20 @@ export async function renderToHTML(
...
@@ -1354,11 +1356,20 @@ export async function renderToHTML(
}
}
if
(
hasConcurrentFeatures
)
{
if
(
hasConcurrentFeatures
)
{
let
renderStream
:
any
// We start rendering the shell earlier, before returning the head tags
// to `documentResult`.
const
content
=
renderContent
()
renderStream
=
await
renderToInitialStream
({
ReactDOMServer
,
element
:
content
,
})
bodyResult
=
async
(
suffix
:
string
)
=>
{
bodyResult
=
async
(
suffix
:
string
)
=>
{
// this must be called inside bodyResult so appWrappers is
// this must be called inside bodyResult so appWrappers is
// up to date when getWrappedApp is called
// up to date when getWrappedApp is called
const
content
=
renderContent
()
const
flushEffectHandler
=
async
()
=>
{
const
flushEffectHandler
=
async
()
=>
{
const
allFlushEffects
=
[
const
allFlushEffects
=
[
styledJsxFlushEffect
,
styledJsxFlushEffect
,
...
@@ -1379,9 +1390,8 @@ export async function renderToHTML(
...
@@ -1379,9 +1390,8 @@ export async function renderToHTML(
return
flushed
return
flushed
}
}
return
await
renderToStream
({
return
await
continueFromInitialStream
({
ReactDOMServer
,
renderStream
,
element
:
content
,
suffix
,
suffix
,
dataStream
:
serverComponentsInlinedTransformStream
?.
readable
,
dataStream
:
serverComponentsInlinedTransformStream
?.
readable
,
generateStaticHTML
:
generateStaticHTML
||
!
hasConcurrentFeatures
,
generateStaticHTML
:
generateStaticHTML
||
!
hasConcurrentFeatures
,
...
...
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