Commit 9d0a1820 authored by Jimmy Lai's avatar Jimmy Lai

refactor: dedupe inlined flight bootstrap encoding

parent 108af215
...@@ -238,17 +238,7 @@ function writeInitialInstructions( ...@@ -238,17 +238,7 @@ function writeInitialInstructions(
scriptStart: string, scriptStart: string,
formState: unknown | null formState: unknown | null
) { ) {
let scriptContents = `(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString( controller.enqueue(encodeInitialFlightData(scriptStart, formState))
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)})`
if (formState != null) {
scriptContents += `;self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})`
}
controller.enqueue(encoder.encode(`${scriptStart}${scriptContents}</script>`))
} }
function writeFlightDataInstruction( function writeFlightDataInstruction(
...@@ -302,6 +292,23 @@ function encodeFlightDataChunk( ...@@ -302,6 +292,23 @@ function encodeFlightDataChunk(
) )
} }
function encodeInitialFlightData(
scriptStart: string,
formState: unknown | null
): Uint8Array {
let scriptContents = `(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)})`
if (formState != null) {
scriptContents += `;self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})`
}
return encoder.encode(`${scriptStart}${scriptContents}</script>`)
}
/** /**
* Creates a Node.js Readable that provides inline script tag chunks for writing * Creates a Node.js Readable that provides inline script tag chunks for writing
* hydration data to the client outside the React render itself. * hydration data to the client outside the React render itself.
...@@ -336,19 +343,7 @@ export function createInlinedDataNodeStream( ...@@ -336,19 +343,7 @@ export function createInlinedDataNodeStream(
try { try {
if (!bootstrapWritten) { if (!bootstrapWritten) {
bootstrapWritten = true bootstrapWritten = true
let scriptContents = `(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString( this.push(encodeInitialFlightData(startScriptTag, formState))
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)})`
if (formState != null) {
scriptContents += `;self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})`
}
this.push(
encoder.encode(`${startScriptTag}${scriptContents}</script>`)
)
} }
try { try {
...@@ -366,19 +361,7 @@ export function createInlinedDataNodeStream( ...@@ -366,19 +361,7 @@ export function createInlinedDataNodeStream(
flush(callback: (error?: Error) => void) { flush(callback: (error?: Error) => void) {
// If no data was ever received, still write the bootstrap // If no data was ever received, still write the bootstrap
if (!bootstrapWritten) { if (!bootstrapWritten) {
let scriptContents = `(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString( this.push(encodeInitialFlightData(startScriptTag, formState))
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BOOTSTRAP])
)})`
if (formState != null) {
scriptContents += `;self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_FORM_STATE, formState])
)})`
}
this.push(
encoder.encode(`${startScriptTag}${scriptContents}</script>`)
)
} }
callback() callback()
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment