-
Notifications
You must be signed in to change notification settings - Fork 47.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fizz] Use identifierPrefix to avoid conflicts within the same response #21037
Merged
sebmarkbage
merged 2 commits into
facebook:master
from
sebmarkbage:fizzidentifierprefix
Mar 22, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,15 +24,25 @@ import invariant from 'shared/invariant'; | |
|
||
// Per response, | ||
export type ResponseState = { | ||
placeholderPrefix: PrecomputedChunk, | ||
segmentPrefix: PrecomputedChunk, | ||
boundaryPrefix: string, | ||
opaqueIdentifierPrefix: PrecomputedChunk, | ||
nextSuspenseID: number, | ||
sentCompleteSegmentFunction: boolean, | ||
sentCompleteBoundaryFunction: boolean, | ||
sentClientRenderFunction: boolean, | ||
}; | ||
|
||
// Allows us to keep track of what we've already written so we can refer back to it. | ||
export function createResponseState(): ResponseState { | ||
export function createResponseState( | ||
identifierPrefix: string = '', | ||
): ResponseState { | ||
return { | ||
placeholderPrefix: stringToPrecomputedChunk(identifierPrefix + 'P:'), | ||
segmentPrefix: stringToPrecomputedChunk(identifierPrefix + 'S:'), | ||
boundaryPrefix: identifierPrefix + 'B:', | ||
opaqueIdentifierPrefix: stringToPrecomputedChunk(identifierPrefix + 'R:'), | ||
nextSuspenseID: 0, | ||
sentCompleteSegmentFunction: false, | ||
sentCompleteBoundaryFunction: false, | ||
|
@@ -68,7 +78,7 @@ function assignAnID( | |
// TODO: This approach doesn't yield deterministic results since this is assigned during render. | ||
const generatedID = responseState.nextSuspenseID++; | ||
return (id.formattedID = stringToPrecomputedChunk( | ||
'B:' + generatedID.toString(16), | ||
responseState.boundaryPrefix + generatedID.toString(16), | ||
)); | ||
} | ||
|
||
|
@@ -160,20 +170,19 @@ export function pushEndInstance( | |
// A placeholder is a node inside a hidden partial tree that can be filled in later, but before | ||
// display. It's never visible to users. | ||
const placeholder1 = stringToPrecomputedChunk('<span id="'); | ||
const placeholder2 = stringToPrecomputedChunk('P:'); | ||
const placeholder3 = stringToPrecomputedChunk('"></span>'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting naming scheme 🙂 |
||
const placeholder2 = stringToPrecomputedChunk('"></span>'); | ||
export function writePlaceholder( | ||
destination: Destination, | ||
responseState: ResponseState, | ||
id: number, | ||
): boolean { | ||
// TODO: This needs to be contextually aware and switch tag since not all parents allow for spans like | ||
// <select> or <tbody>. E.g. suspending a component that renders a table row. | ||
writeChunk(destination, placeholder1); | ||
// TODO: Use the identifierPrefix option to make the prefix configurable. | ||
writeChunk(destination, placeholder2); | ||
writeChunk(destination, responseState.placeholderPrefix); | ||
const formattedID = stringToChunk(id.toString(16)); | ||
writeChunk(destination, formattedID); | ||
return writeChunk(destination, placeholder3); | ||
return writeChunk(destination, placeholder2); | ||
} | ||
|
||
// Suspense boundaries are encoded as comments. | ||
|
@@ -207,20 +216,19 @@ export function writeEndSuspenseBoundary(destination: Destination): boolean { | |
} | ||
|
||
const startSegment = stringToPrecomputedChunk('<div hidden id="'); | ||
const startSegment2 = stringToPrecomputedChunk('S:'); | ||
const startSegment3 = stringToPrecomputedChunk('">'); | ||
const startSegment2 = stringToPrecomputedChunk('">'); | ||
const endSegment = stringToPrecomputedChunk('</div>'); | ||
export function writeStartSegment( | ||
destination: Destination, | ||
responseState: ResponseState, | ||
id: number, | ||
): boolean { | ||
// TODO: What happens with special children like <tr> if they're inserted in a div? Maybe needs contextually aware containers. | ||
writeChunk(destination, startSegment); | ||
// TODO: Use the identifierPrefix option to make the prefix configurable. | ||
writeChunk(destination, startSegment2); | ||
writeChunk(destination, responseState.segmentPrefix); | ||
const formattedID = stringToChunk(id.toString(16)); | ||
writeChunk(destination, formattedID); | ||
return writeChunk(destination, startSegment3); | ||
return writeChunk(destination, startSegment2); | ||
} | ||
export function writeEndSegment(destination: Destination): boolean { | ||
return writeChunk(destination, endSegment); | ||
|
@@ -349,12 +357,10 @@ const clientRenderFunction = | |
'function $RX(b){if(b=document.getElementById(b)){do b=b.previousSibling;while(8!==b.nodeType||"$?"!==b.data);b.data="$!";b._reactRetry&&b._reactRetry()}}'; | ||
|
||
const completeSegmentScript1Full = stringToPrecomputedChunk( | ||
'<script>' + completeSegmentFunction + ';$RS("S:', | ||
); | ||
const completeSegmentScript1Partial = stringToPrecomputedChunk( | ||
'<script>$RS("S:', | ||
'<script>' + completeSegmentFunction + ';$RS("', | ||
); | ||
const completeSegmentScript2 = stringToPrecomputedChunk('","P:'); | ||
const completeSegmentScript1Partial = stringToPrecomputedChunk('<script>$RS("'); | ||
const completeSegmentScript2 = stringToPrecomputedChunk('","'); | ||
const completeSegmentScript3 = stringToPrecomputedChunk('")</script>'); | ||
|
||
export function writeCompletedSegmentInstruction( | ||
|
@@ -370,10 +376,11 @@ export function writeCompletedSegmentInstruction( | |
// Future calls can just reuse the same function. | ||
writeChunk(destination, completeSegmentScript1Partial); | ||
} | ||
// TODO: Use the identifierPrefix option to make the prefix configurable. | ||
writeChunk(destination, responseState.segmentPrefix); | ||
const formattedID = stringToChunk(contentSegmentID.toString(16)); | ||
writeChunk(destination, formattedID); | ||
writeChunk(destination, completeSegmentScript2); | ||
writeChunk(destination, responseState.placeholderPrefix); | ||
writeChunk(destination, formattedID); | ||
return writeChunk(destination, completeSegmentScript3); | ||
} | ||
|
@@ -384,7 +391,7 @@ const completeBoundaryScript1Full = stringToPrecomputedChunk( | |
const completeBoundaryScript1Partial = stringToPrecomputedChunk( | ||
'<script>$RC("', | ||
); | ||
const completeBoundaryScript2 = stringToPrecomputedChunk('","S:'); | ||
const completeBoundaryScript2 = stringToPrecomputedChunk('","'); | ||
const completeBoundaryScript3 = stringToPrecomputedChunk('")</script>'); | ||
|
||
export function writeCompletedBoundaryInstruction( | ||
|
@@ -401,7 +408,6 @@ export function writeCompletedBoundaryInstruction( | |
// Future calls can just reuse the same function. | ||
writeChunk(destination, completeBoundaryScript1Partial); | ||
} | ||
// TODO: Use the identifierPrefix option to make the prefix configurable. | ||
const formattedBoundaryID = boundaryID.formattedID; | ||
invariant( | ||
formattedBoundaryID !== null, | ||
|
@@ -410,6 +416,7 @@ export function writeCompletedBoundaryInstruction( | |
const formattedContentID = stringToChunk(contentSegmentID.toString(16)); | ||
writeChunk(destination, formattedBoundaryID); | ||
writeChunk(destination, completeBoundaryScript2); | ||
writeChunk(destination, responseState.segmentPrefix); | ||
writeChunk(destination, formattedContentID); | ||
return writeChunk(destination, completeBoundaryScript3); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is a little difficult to do correctly in practice because there's no explicit signal when a root is flushed. I think the way you'd do that is explicitly wait to start writing until the root is signaled as ready, and then startWriting and assume that it's done writing after a setImmediate tick or something but that's a little flawed. What you probably want is a way to explicitly write the whole root synchronously after it is done loading.
The main pattern we want to support is the legacy synchronous render. This test shows that we can support it for streaming multiple containers though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently startWriting will synchronously emit the root if it's available. We can probably preserve that property.