Created
August 20, 2024 11:42
-
-
Save sibelius/00440c0f68af5d5de7a1a55ad26b29d5 to your computer and use it in GitHub Desktop.
renderToPipeableStreamPromise
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
export const renderToPipeableStreamPromise = async ( | |
tree: ReactElement, | |
ctx: Context, | |
next: Next, | |
) => { | |
const writableStream = new WritableAsPromise(); | |
let didError = false; | |
const { pipe, abort } = renderToPipeableStream(tree, { | |
onError(error) { | |
writableStream._destroy(error); | |
abort(); | |
ctx.status = 500; | |
// eslint-disable-next-line no-console | |
console.error(error); | |
}, | |
onShellReady() { | |
ctx.status = didError ? 500 : 200; | |
ctx.set('Content-Type', 'text/html'); | |
writableStream._write(ctx.body ?? '', undefined, next); | |
pipe(writableStream); | |
}, | |
onShellError(error) { | |
writableStream._destroy(error, next); | |
abort(); | |
didError = true; | |
ctx.set('Content-Type', 'text/html'); | |
ctx.body = '<!doctype html><p>Loading...</p><script src="clientrender.js"></script>'; | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment