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
f4002fb9
Commit
f4002fb9
authored
Aug 20, 2025
by
Janka Uryga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tagged serialization format
parent
5a224361
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
155 additions
and
65 deletions
+155
-65
length-encoding.test.ts
packages/next/src/server/app-render/length-encoding.test.ts
+27
-1
length-encoding.ts
packages/next/src/server/app-render/length-encoding.ts
+38
-0
postponed-state.ts
packages/next/src/server/app-render/postponed-state.ts
+90
-64
No files found.
packages/next/src/server/app-render/length-encoding.test.ts
View file @
f4002fb9
import
{
lengthEncodeTuple
,
lengthDecodeTuple
}
from
'./length-encoding'
import
{
lengthEncodeTuple
,
lengthDecodeTuple
,
lengthEncodeTupleWithTag
,
lengthDecodeTupleWithTag
,
type
TaggedStringTuple
,
}
from
'./length-encoding'
const
cases
:
{
description
:
string
;
input
:
string
[]
}[]
=
[
const
cases
:
{
description
:
string
;
input
:
string
[]
}[]
=
[
[],
[],
...
@@ -32,3 +38,23 @@ it.each(cases)(
...
@@ -32,3 +38,23 @@ it.each(cases)(
expect
(
decoded
).
toEqual
(
input
)
expect
(
decoded
).
toEqual
(
input
)
}
}
)
)
enum
MyTag
{
A
=
0
,
B
=
1
,
C
=
9
,
}
describe
(
'tags'
,
()
=>
{
describe
.
each
([
MyTag
.
A
,
MyTag
.
B
,
MyTag
.
C
])(
'tag value - %d'
,
(
tag
)
=>
{
it
.
each
(
cases
)(
'can encode and decode tuples with a tag without changes - $description'
,
({
input
:
inputWithoutTag
})
=>
{
const
input
:
TaggedStringTuple
=
[
tag
,
...
inputWithoutTag
]
const
encoded
:
string
=
lengthEncodeTupleWithTag
(
input
)
const
decoded
:
TaggedStringTuple
=
lengthDecodeTupleWithTag
(
encoded
)
expect
(
decoded
).
toEqual
(
input
)
}
)
})
})
packages/next/src/server/app-render/length-encoding.ts
View file @
f4002fb9
...
@@ -93,3 +93,41 @@ export function lengthDecodeTuple(encoded: string): string[] {
...
@@ -93,3 +93,41 @@ export function lengthDecodeTuple(encoded: string): string[] {
return
parts
return
parts
}
}
export
type
TaggedStringTuple
=
[
tag
:
number
,
...
rest
:
string
[]]
/**
* Encodes a tuple of strings into a single string,
* where each part except the last is prefixed with its length.
* */
export
function
lengthEncodeTupleWithTag
([
tag
,
...
rest
]:
TaggedStringTuple
)
{
// Technically, we could allow more by using letters, but we don't need that now.
if
(
tag
<
0
||
tag
>
9
)
{
throw
new
InvariantError
(
'Tags that cannot be encoded as a single digit are not supported'
)
}
return
tag
+
lengthEncodeTuple
(
rest
)
}
const
ASCII_0
=
'0'
.
charCodeAt
(
0
)
const
ASCII_9
=
'9'
.
charCodeAt
(
0
)
export
function
lengthDecodeTupleWithTag
(
encoded
:
string
):
TaggedStringTuple
{
if
(
!
encoded
)
{
throw
new
InvariantError
(
'Expected tag but got empty input'
)
}
// The tag is a single ASCII digit.
const
tagCharCode
=
encoded
.
charCodeAt
(
0
)
if
(
tagCharCode
<
ASCII_0
||
tagCharCode
>
ASCII_9
)
{
throw
new
InvariantError
(
`Expected tag to be a numeric digit, but got '
${
encoded
[
0
]}
'`
)
}
const
tag
=
tagCharCode
-
ASCII_0
const
parts
=
lengthDecodeTuple
(
encoded
.
slice
(
1
))
as
(
string
|
number
)[]
parts
.
unshift
(
tag
)
return
parts
as
TaggedStringTuple
}
packages/next/src/server/app-render/postponed-state.ts
View file @
f4002fb9
...
@@ -8,7 +8,10 @@ import {
...
@@ -8,7 +8,10 @@ import {
type
RenderResumeDataCache
,
type
RenderResumeDataCache
,
}
from
'../resume-data-cache/resume-data-cache'
}
from
'../resume-data-cache/resume-data-cache'
import
{
stringifyResumeDataCache
}
from
'../resume-data-cache/resume-data-cache'
import
{
stringifyResumeDataCache
}
from
'../resume-data-cache/resume-data-cache'
import
{
lengthDecodeTuple
,
lengthEncodeTuple
}
from
'./length-encoding'
import
{
lengthDecodeTupleWithTag
,
lengthEncodeTupleWithTag
,
}
from
'./length-encoding'
export
enum
DynamicState
{
export
enum
DynamicState
{
/**
/**
...
@@ -73,17 +76,39 @@ export type PostponedState =
...
@@ -73,17 +76,39 @@ export type PostponedState =
|
DynamicDataPostponedState
|
DynamicDataPostponedState
|
DynamicHTMLPostponedState
|
DynamicHTMLPostponedState
function
serializeStateParts
(
state
:
SerializedStateParts
)
{
return
lengthEncodeTupleWithTag
(
state
)
}
function
deserializeStateParts
(
serialized
:
string
)
{
return
lengthDecodeTupleWithTag
(
serialized
)
as
SerializedStateParts
}
type
SerializedStateParts
=
type
SerializedStateParts
=
|
SerializedDynamicData
|
SerializedDynamicData
|
SerializedDynamicHTML
|
SerializedDynamicHTML
|
SerializedDynamicHTMLWithReplacements
|
SerializedDynamicHTMLWithReplacements
type
SerializedDynamicData
=
[
resumeDataCache
:
string
]
enum
SerializedStateTag
{
type
SerializedDynamicHTML
=
[
postponed
:
string
,
resumeDataCache
:
string
]
DynamicData
=
0
,
type
SerializedDynamicHTMLWithReplacements
=
[
DynamicHTML
=
1
,
replacements
:
string
,
DynamicHTMLWithReplacements
=
2
,
}
type
SerializedDynamicData
=
[
tag
:
SerializedStateTag
.
DynamicData
,
resumeDataCache
:
string
,
]
type
SerializedDynamicHTML
=
[
tag
:
SerializedStateTag
.
DynamicHTML
,
resumeDataCache
:
string
,
postponed
:
string
,
postponed
:
string
,
]
type
SerializedDynamicHTMLWithReplacements
=
[
tag
:
SerializedStateTag
.
DynamicHTMLWithReplacements
,
resumeDataCache
:
string
,
resumeDataCache
:
string
,
postponed
:
string
,
replacements
:
string
,
]
]
export
async
function
getDynamicHTMLPostponedState
(
export
async
function
getDynamicHTMLPostponedState
(
...
@@ -96,32 +121,35 @@ export async function getDynamicHTMLPostponedState(
...
@@ -96,32 +121,35 @@ export async function getDynamicHTMLPostponedState(
const
dataString
=
JSON
.
stringify
(
data
)
const
dataString
=
JSON
.
stringify
(
data
)
if
(
!
fallbackRouteParams
||
fallbackRouteParams
.
size
===
0
)
{
if
(
!
fallbackRouteParams
||
fallbackRouteParams
.
size
===
0
)
{
return
lengthEncodeTuple
([
return
serializeStateParts
([
dataString
,
SerializedStateTag
.
DynamicHTML
,
await
stringifyResumeDataCache
(
await
stringifyResumeDataCache
(
createRenderResumeDataCache
(
resumeDataCache
)
createRenderResumeDataCache
(
resumeDataCache
)
),
),
]
satisfies
SerializedDynamicHTML
)
dataString
,
])
}
}
const
replacements
:
Array
<
[
string
,
string
]
>
=
Array
.
from
(
fallbackRouteParams
)
const
replacements
:
Array
<
[
string
,
string
]
>
=
Array
.
from
(
fallbackRouteParams
)
const
replacementsString
=
JSON
.
stringify
(
replacements
)
const
replacementsString
=
JSON
.
stringify
(
replacements
)
return
lengthEncodeTuple
([
return
serializeStateParts
([
replacementsString
,
SerializedStateTag
.
DynamicHTMLWithReplacements
,
dataString
,
await
stringifyResumeDataCache
(
resumeDataCache
),
await
stringifyResumeDataCache
(
resumeDataCache
),
]
satisfies
SerializedDynamicHTMLWithReplacements
)
dataString
,
replacementsString
,
])
}
}
export
async
function
getDynamicDataPostponedState
(
export
async
function
getDynamicDataPostponedState
(
resumeDataCache
:
PrerenderResumeDataCache
|
RenderResumeDataCache
resumeDataCache
:
PrerenderResumeDataCache
|
RenderResumeDataCache
):
Promise
<
string
>
{
):
Promise
<
string
>
{
return
lengthEncodeTuple
([
return
serializeStateParts
([
SerializedStateTag
.
DynamicData
,
await
stringifyResumeDataCache
(
await
stringifyResumeDataCache
(
createRenderResumeDataCache
(
resumeDataCache
)
createRenderResumeDataCache
(
resumeDataCache
)
),
),
]
satisfies
SerializedDynamicData
)
])
}
}
export
function
parsePostponedState
(
export
function
parsePostponedState
(
...
@@ -129,10 +157,11 @@ export function parsePostponedState(
...
@@ -129,10 +157,11 @@ export function parsePostponedState(
params
:
Params
|
undefined
params
:
Params
|
undefined
):
PostponedState
{
):
PostponedState
{
try
{
try
{
const
parts
=
lengthDecodeTuple
(
state
)
as
SerializedStateParts
const
parts
=
deserializeStateParts
(
state
)
if
(
parts
.
length
===
1
)
{
const
tag
=
parts
[
0
]
parts
satisfies
SerializedDynamicData
switch
(
tag
)
{
const
[
resumeDataCacheString
]
=
parts
case
SerializedStateTag
.
DynamicData
:
{
const
[,
resumeDataCacheString
]
=
parts
const
renderResumeDataCache
=
createRenderResumeDataCache
(
const
renderResumeDataCache
=
createRenderResumeDataCache
(
resumeDataCacheString
resumeDataCacheString
)
)
...
@@ -140,31 +169,26 @@ export function parsePostponedState(
...
@@ -140,31 +169,26 @@ export function parsePostponedState(
type
:
DynamicState
.
DATA
,
type
:
DynamicState
.
DATA
,
renderResumeDataCache
,
renderResumeDataCache
,
}
}
}
else
if
(
parts
.
length
===
2
||
parts
.
length
===
3
)
{
let
replacementsString
:
string
|
null
=
null
,
postponedString
:
string
,
resumeDataCacheString
:
string
if
(
parts
.
length
===
2
)
{
parts
satisfies
SerializedDynamicHTML
;[
postponedString
,
resumeDataCacheString
]
=
parts
}
else
{
parts
satisfies
SerializedDynamicHTMLWithReplacements
;[
replacementsString
,
postponedString
,
resumeDataCacheString
]
=
parts
}
}
case
SerializedStateTag
.
DynamicHTML
:
case
SerializedStateTag
.
DynamicHTMLWithReplacements
:
{
// These two variants mostly overlap, except for the last element
let
[,
resumeDataCacheString
,
postponedString
]
=
parts
const
renderResumeDataCache
=
createRenderResumeDataCache
(
const
renderResumeDataCache
=
createRenderResumeDataCache
(
resumeDataCacheString
resumeDataCacheString
)
)
try
{
try
{
if
(
replacementsString
!==
null
)
{
if
(
tag
===
SerializedStateTag
.
DynamicHTMLWithReplacements
)
{
const
replacements
=
JSON
.
parse
(
replacementsString
)
as
ReadonlyArray
<
const
replacementsString
=
parts
[
3
]
[
string
,
string
]
const
replacements
=
JSON
.
parse
(
>
replacementsString
)
as
ReadonlyArray
<
[
string
,
string
]
>
for
(
const
[
key
,
searchValue
]
of
replacements
)
{
for
(
const
[
key
,
searchValue
]
of
replacements
)
{
const
value
=
params
?.[
key
]
??
''
const
value
=
params
?.[
key
]
??
''
const
replaceValue
=
Array
.
isArray
(
value
)
?
value
.
join
(
'/'
)
:
value
const
replaceValue
=
Array
.
isArray
(
value
)
?
value
.
join
(
'/'
)
:
value
postponedString
=
postponedString
.
replaceAll
(
postponedString
=
postponedString
.
replaceAll
(
searchValue
,
searchValue
,
replaceValue
replaceValue
...
@@ -181,9 +205,11 @@ export function parsePostponedState(
...
@@ -181,9 +205,11 @@ export function parsePostponedState(
console
.
error
(
'Failed to parse postponed state'
,
err
)
console
.
error
(
'Failed to parse postponed state'
,
err
)
return
{
type
:
DynamicState
.
DATA
,
renderResumeDataCache
}
return
{
type
:
DynamicState
.
DATA
,
renderResumeDataCache
}
}
}
}
else
{
}
default
:
{
parts
satisfies
never
parts
satisfies
never
throw
new
InvariantError
(
'Postponed state tuple has invalid length'
)
throw
new
InvariantError
(
`Invalid postponed state tag:
${
tag
}
`
)
}
}
}
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
error
(
'Failed to parse postponed state'
,
err
)
console
.
error
(
'Failed to parse postponed state'
,
err
)
...
...
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