-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-18): Add streaming implementation
- Loading branch information
Showing
11 changed files
with
232 additions
and
85 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { renderToPipeableStream } from "react-dom/server" | ||
import { ArtsyResponse } from "Server/middleware/artsyExpress" | ||
import { Transform } from "stream" | ||
|
||
const STREAM_TIMEOUT = 5000 | ||
|
||
export function renderToStream(jsx, sheet, res: ArtsyResponse) { | ||
let didError = false | ||
|
||
const decoder = new TextDecoder("utf-8") | ||
|
||
const stream = new Transform({ | ||
objectMode: true, | ||
transform(chunk, encoding, callback) { | ||
const renderedHtml = | ||
chunk instanceof Uint8Array | ||
? decoder.decode(chunk, { stream: true }) | ||
: chunk.toString(encoding || "utf8") | ||
|
||
const styledCSS = sheet._emitSheetCSS() | ||
const CLOSING_TAG_R = /<\/[a-z]*>/i | ||
|
||
sheet.instance.clearTag() | ||
|
||
// Inject CSS into HTML | ||
if (/<\/head>/.test(renderedHtml)) { | ||
const replacedHtml = renderedHtml.replace( | ||
"</head>", | ||
`${styledCSS}</head>` | ||
) | ||
this.push(replacedHtml) | ||
} else if (CLOSING_TAG_R.test(renderedHtml)) { | ||
const execResult = CLOSING_TAG_R.exec(renderedHtml) as RegExpExecArray | ||
const endOfClosingTag = execResult.index + execResult[0].length | ||
const before = renderedHtml.slice(0, endOfClosingTag) | ||
const after = renderedHtml.slice(endOfClosingTag) | ||
|
||
this.push(before + styledCSS + after) | ||
} else { | ||
this.push(styledCSS + renderedHtml) | ||
} | ||
|
||
callback() | ||
}, | ||
}) | ||
|
||
let streamTimeout: NodeJS.Timeout | ||
|
||
const { pipe, abort } = renderToPipeableStream(jsx, { | ||
onError: error => { | ||
didError = true | ||
console.error("error", error) | ||
}, | ||
onShellError: error => { | ||
didError = true | ||
console.log("shell error", error) | ||
}, | ||
onShellReady: () => { | ||
res.statusCode = didError ? 500 : 200 | ||
res.setHeader("Content-Type", "text/html; charset=utf-8") | ||
pipe(stream) | ||
}, | ||
onAllReady: () => { | ||
clearTimeout(streamTimeout) | ||
}, | ||
}) | ||
|
||
// Abandon and switch to client rendering if enough time passes. | ||
streamTimeout = setTimeout(() => { | ||
abort() | ||
}, STREAM_TIMEOUT) | ||
|
||
return stream | ||
} |
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
Oops, something went wrong.