-
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
10 changed files
with
214 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { renderToPipeableStream } from "react-dom/server" | ||
import { ArtsyResponse } from "Server/middleware/artsyExpress" | ||
import { Transform } from "stream" | ||
|
||
const STREAM_TIMEOUT = 10000 | ||
|
||
export function renderToStream(jsx, sheet, res: ArtsyResponse) { | ||
let didError = false | ||
|
||
const readerWriter = new Transform({ | ||
objectMode: true, | ||
transform(chunk, encoding, callback) { | ||
const renderedHtml = | ||
chunk instanceof Uint8Array | ||
? Buffer.from(chunk).toString(encoding || "utf8") | ||
: Buffer.isBuffer(chunk) | ||
? chunk.toString(encoding || "utf8") | ||
: chunk | ||
|
||
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() | ||
}, | ||
}) | ||
|
||
const stream = 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") | ||
stream.pipe(readerWriter) | ||
}, | ||
}) | ||
|
||
// Abandon and switch to client rendering if enough time passes. | ||
setTimeout(() => { | ||
stream.abort() | ||
}, STREAM_TIMEOUT) | ||
|
||
return readerWriter | ||
} |
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.