Unverified Commit b75b2f02 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Improve error handling in the SSR middleware (#31057)

This PR improves error handling in the SSR middleware. Previously the response was sent out synchronously, and and errors were silently swallowed. There was no `.catch` for `renderToHTML`. This changes the middleware to be asynchronous, which waits until the initial Document to be rendered correctly and then starts the streaming. 

With this change we can also send correct status code when there're immediate errors before Fizz.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 0307ba0d
...@@ -57,10 +57,6 @@ export default async function middlewareRSCLoader(this: any) { ...@@ -57,10 +57,6 @@ export default async function middlewareRSCLoader(this: any) {
throw new Error('Your page must export a \`default\` component') throw new Error('Your page must export a \`default\` component')
} }
function renderError(err, status) {
return new Response(err.toString(), {status})
}
function wrapReadable(readable) { function wrapReadable(readable) {
const encoder = new TextEncoder() const encoder = new TextEncoder()
const transformStream = new TransformStream() const transformStream = new TransformStream()
...@@ -104,15 +100,10 @@ export default async function middlewareRSCLoader(this: any) { ...@@ -104,15 +100,10 @@ export default async function middlewareRSCLoader(this: any) {
const Component = Page` const Component = Page`
} }
function render(request) { async function render(request) {
const url = request.nextUrl const url = request.nextUrl
const query = Object.fromEntries(url.searchParams) const query = Object.fromEntries(url.searchParams)
if (Document.getInitialProps) {
const err = new Error('Document.getInitialProps is not supported with server components, please remove it from pages/_document')
return renderError(err, 500)
}
// Preflight request // Preflight request
if (request.method === 'HEAD') { if (request.method === 'HEAD') {
return new Response('OK.', { return new Response('OK.', {
...@@ -172,18 +163,27 @@ export default async function middlewareRSCLoader(this: any) { ...@@ -172,18 +163,27 @@ export default async function middlewareRSCLoader(this: any) {
const writer = transformStream.writable.getWriter() const writer = transformStream.writable.getWriter()
const encoder = new TextEncoder() const encoder = new TextEncoder()
renderToHTML( try {
{ url: url.pathname }, const result = await renderToHTML(
{}, { url: url.pathname },
url.pathname, {},
query, url.pathname,
renderOpts query,
).then(result => { renderOpts
)
result.pipe({ result.pipe({
write: str => writer.write(encoder.encode(str)), write: str => writer.write(encoder.encode(str)),
end: () => writer.close() end: () => writer.close()
}) })
}) } catch (err) {
return new Response(
(err || 'An error occurred while rendering ' + url.pathname + '.').toString(),
{
status: 500,
headers: { 'x-middleware-ssr': '1' }
}
)
}
return new Response(transformStream.readable, { return new Response(transformStream.readable, {
headers: { 'x-middleware-ssr': '1' } headers: { 'x-middleware-ssr': '1' }
......
...@@ -190,7 +190,7 @@ const documentSuite = { ...@@ -190,7 +190,7 @@ const documentSuite = {
expect(res.status).toBe(500) expect(res.status).toBe(500)
expect(html).toContain( expect(html).toContain(
'Document.getInitialProps is not supported with server components, please remove it from pages/_document' 'Error: `getInitialProps` in Document component is not supported with `concurrentFeatures` enabled.'
) )
}) })
}, },
......
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