Skip to content

Commit 53c8fac

Browse files
committed
How does this even make sense?
1 parent a67fe1d commit 53c8fac

File tree

2 files changed

+65
-39
lines changed

2 files changed

+65
-39
lines changed

packages/next/src/server/app-render/app-render.tsx

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,17 +3243,19 @@ async function renderWithRestartOnCacheMissInDev(
32433243
// where sync IO does not cause aborts, so it's okay if it happens before render.
32443244
const initialRscPayload = await getPayload(requestStore)
32453245

3246-
console.log('\n\n##########################################################')
3247-
console.log(
3246+
process.stdout.write(
3247+
'\n\n##########################################################\n'
3248+
)
3249+
process.stdout.write(
32483250
`[app-render] :: rendering ${requestStore.url.pathname} (${ctx.workStore.route})\n`
32493251
)
32503252
const maybeInitialServerStream = await workUnitAsyncStorage.run(
32513253
requestStore,
32523254
() =>
32533255
pipelineInSequentialTasks(
32543256
() => {
3255-
console.log(
3256-
'=================== [initial] Static ==================='
3257+
process.stdout.write(
3258+
'=================== [initial] Static ===================\n'
32573259
)
32583260
// Static stage
32593261
initialStageController.advanceStage(RenderStage.Static)
@@ -3292,8 +3294,8 @@ async function renderWithRestartOnCacheMissInDev(
32923294

32933295
if (initialStageController.currentStage === RenderStage.Abandoned) {
32943296
// If we abandoned the render in the static stage, we won't proceed further.
3295-
console.log(
3296-
'[app-render] initial render was abandoned due to sync IO in the static stage'
3297+
process.stdout.write(
3298+
'[app-render] initial render was abandoned due to sync IO in the static stage\n'
32973299
)
32983300
return null
32993301
}
@@ -3306,15 +3308,15 @@ async function renderWithRestartOnCacheMissInDev(
33063308
// Regardless of whether we are going to abandon this
33073309
// render we need the unblock runtime b/c it's essential
33083310
// filling caches.
3309-
console.log(
3310-
'[app-render] abandoning initial render due to a cache miss in the static stage'
3311+
process.stdout.write(
3312+
'[app-render] abandoning initial render due to a cache miss in the static stage\n'
33113313
)
33123314
initialStageController.abandonRender()
33133315
return null
33143316
}
33153317

3316-
console.log(
3317-
'=================== [initial] Runtime ==================='
3318+
process.stdout.write(
3319+
'=================== [initial] Runtime ===================\n'
33183320
)
33193321

33203322
initialStageController.advanceStage(RenderStage.Runtime)
@@ -3330,8 +3332,8 @@ async function renderWithRestartOnCacheMissInDev(
33303332
stream !== null &&
33313333
initialStageController.currentStage === RenderStage.Abandoned
33323334
) {
3333-
console.log(
3334-
'[app-render] initial render was abandoned due to sync IO in the runtime stage'
3335+
process.stdout.write(
3336+
'[app-render] initial render was abandoned due to sync IO in the runtime stage\n'
33353337
)
33363338
}
33373339
// If we abandoned the render in the static or runtime stage, we won't proceed further.
@@ -3343,15 +3345,15 @@ async function renderWithRestartOnCacheMissInDev(
33433345
// We won't advance the stage, and thus leave dynamic APIs hanging,
33443346
// because they won't be cached anyway, so it'd be wasted work.
33453347
if (cacheSignal.hasPendingReads()) {
3346-
console.log(
3347-
'[app-render] abandoning initial render due to a cache miss in the runtime stage'
3348+
process.stdout.write(
3349+
'[app-render] abandoning initial render due to a cache miss in the runtime stage\n'
33483350
)
33493351
initialStageController.abandonRender()
33503352
return null
33513353
}
33523354

3353-
console.log(
3354-
'=================== [initial] Dynamic ==================='
3355+
process.stdout.write(
3356+
'=================== [initial] Dynamic ===================\n'
33553357
)
33563358
// Regardless of whether we are going to abandon this
33573359
// render we need the unblock runtime b/c it's essential
@@ -3395,7 +3397,7 @@ async function renderWithRestartOnCacheMissInDev(
33953397
await cacheSignal.cacheReady()
33963398
initialReactController.abort()
33973399

3398-
console.log('*********** restarting render ***********')
3400+
process.stdout.write('*********** restarting render ***********\n')
33993401

34003402
//===============================================
34013403
// Final render (restarted)
@@ -3444,7 +3446,9 @@ async function renderWithRestartOnCacheMissInDev(
34443446
const finalServerStream = await workUnitAsyncStorage.run(requestStore, () =>
34453447
pipelineInSequentialTasks(
34463448
() => {
3447-
console.log('=================== [final] Static ===================')
3449+
process.stdout.write(
3450+
'=================== [final] Static ===================\n'
3451+
)
34483452
// Static stage
34493453
finalStageController.advanceStage(RenderStage.Static)
34503454

@@ -3472,22 +3476,26 @@ async function renderWithRestartOnCacheMissInDev(
34723476
},
34733477
(stream) => {
34743478
if (finalStageController.currentStage !== RenderStage.Static) {
3475-
console.log(
3476-
`[app-render] stage was advanced to ${RenderStage[finalStageController.currentStage]} before reaching the runtime stage`
3479+
process.stdout.write(
3480+
`[app-render] stage was advanced to ${RenderStage[finalStageController.currentStage]} before reaching the runtime stage\n`
34773481
)
34783482
}
3479-
console.log('=================== [final] Runtime ===================')
3483+
process.stdout.write(
3484+
'=================== [final] Runtime ===================\n'
3485+
)
34803486
// Runtime stage
34813487
finalStageController.advanceStage(RenderStage.Runtime)
34823488
return stream
34833489
},
34843490
(stream) => {
34853491
if (finalStageController.currentStage !== RenderStage.Runtime) {
3486-
console.log(
3487-
`[app-render] stage was advanced to ${RenderStage[finalStageController.currentStage]} before reaching the dynamic stage`
3492+
process.stdout.write(
3493+
`[app-render] stage was advanced to ${RenderStage[finalStageController.currentStage]} before reaching the dynamic stage\n`
34883494
)
34893495
}
3490-
console.log('=================== [final] Dynamic ===================')
3496+
process.stdout.write(
3497+
'=================== [final] Dynamic ===================\n'
3498+
)
34913499
// Dynamic stage
34923500
finalStageController.advanceStage(RenderStage.Dynamic)
34933501
return stream
@@ -3542,7 +3550,10 @@ async function accumulateStreamChunks(
35423550
cancel()
35433551
break
35443552
}
3545-
switch (stageController.currentStage) {
3553+
process.stdout.write(
3554+
`CHUNK ${stageController.chunkStage}\n${new TextDecoder().decode(value)}\n`
3555+
)
3556+
switch (stageController.chunkStage) {
35463557
case RenderStage.Before:
35473558
throw new InvariantError(
35483559
'Unexpected stream chunk while in Before stage'
@@ -3561,7 +3572,7 @@ async function accumulateStreamChunks(
35613572
// so there's no need to accumulate them
35623573
break
35633574
default:
3564-
stageController.currentStage satisfies never
3575+
stageController.chunkStage satisfies never
35653576
break
35663577
}
35673578
}
@@ -3700,32 +3711,32 @@ async function spawnStaticShellValidationInDev(
37003711
const logChunks = (chunks: Array<Uint8Array>) => {
37013712
const textDecoder = new TextDecoder()
37023713
for (const chunk of chunks) {
3703-
console.log(textDecoder.decode(chunk))
3714+
process.stdout.write(textDecoder.decode(chunk) + '\n')
37043715
}
37053716
}
3706-
console.log(`Static chunks (${staticServerChunks.length})`)
3707-
console.log('------------------------')
3717+
process.stdout.write(`Static chunks (${staticServerChunks.length})\n`)
3718+
process.stdout.write('------------------------\n')
37083719
logChunks(staticServerChunks)
3709-
console.log('------------------------')
3720+
process.stdout.write('------------------------\n')
37103721

37113722
const runtimeOnlyChunks = runtimeServerChunks.slice(
37123723
staticServerChunks.length
37133724
)
3714-
console.log('\n')
3715-
console.log(`Runtime chunks (${runtimeOnlyChunks.length})`)
3716-
console.log('------------------------')
3725+
process.stdout.write('\n\n')
3726+
process.stdout.write(`Runtime chunks (${runtimeOnlyChunks.length})\n`)
3727+
process.stdout.write('------------------------\n')
37173728
logChunks(runtimeOnlyChunks)
3718-
console.log('------------------------')
3729+
process.stdout.write('------------------------\n')
37193730

37203731
const dynamicOnlyChunks = dynamicServerChunks.slice(
37213732
runtimeServerChunks.length
37223733
)
3723-
console.log('\n')
3724-
console.log(`Dynamic chunks (${dynamicOnlyChunks.length})`)
3725-
console.log('------------------------')
3734+
process.stdout.write('\n\n')
3735+
process.stdout.write(`Dynamic chunks (${dynamicOnlyChunks.length})\n`)
3736+
process.stdout.write('------------------------\n')
37263737
logChunks(dynamicOnlyChunks)
3727-
console.log('------------------------')
3728-
console.log('\n\n')
3738+
process.stdout.write('------------------------\n')
3739+
process.stdout.write('\n\n\n')
37293740
}
37303741

37313742
const { allowEmptyStaticShell = false } = renderOpts

packages/next/src/server/app-render/staged-rendering.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export type NonStaticRenderStage = RenderStage.Runtime | RenderStage.Dynamic
1414
export class StagedRenderingController {
1515
currentStage: RenderStage = RenderStage.Before
1616

17+
// chunkStage lags behind currentStage, updated on process.nextTick after
18+
// stage transitions. This allows React to emit additional chunks after stage
19+
// listeners fire, at which point the current stage might already be ahead.
20+
chunkStage: RenderStage = RenderStage.Before
21+
1722
staticInterruptReason: Error | null = null
1823
runtimeInterruptReason: Error | null = null
1924
staticStageEndTime: number = Infinity
@@ -168,11 +173,13 @@ export class StagedRenderingController {
168173
switch (currentStage) {
169174
case RenderStage.Static: {
170175
this.currentStage = RenderStage.Abandoned
176+
this.chunkStage = RenderStage.Abandoned
171177
this.resolveRuntimeStage()
172178
return
173179
}
174180
case RenderStage.Runtime: {
175181
this.currentStage = RenderStage.Abandoned
182+
this.chunkStage = RenderStage.Abandoned
176183
return
177184
}
178185
case RenderStage.Dynamic:
@@ -197,6 +204,14 @@ export class StagedRenderingController {
197204
let currentStage = this.currentStage
198205
this.currentStage = stage
199206

207+
if (stage === RenderStage.Static) {
208+
this.chunkStage = stage
209+
} else {
210+
process.nextTick(() => {
211+
this.chunkStage = stage
212+
})
213+
}
214+
200215
if (currentStage < RenderStage.Runtime && stage >= RenderStage.Runtime) {
201216
this.staticStageEndTime = performance.now() + performance.timeOrigin
202217
this.resolveRuntimeStage()

0 commit comments

Comments
 (0)