From 0b3db9173174c7b6ee5e1180441c34d43723982d Mon Sep 17 00:00:00 2001 From: Naoyuki Kanezawa Date: Thu, 26 Jan 2017 20:06:50 +0900 Subject: [PATCH] Fix handling finished response (#889) * allows to not return props if response is already finished on getInitialProps * check res.finished after getInitialProps call of Document --- lib/utils.js | 2 +- server/render.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 56047419062a5..b1757f9878496 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -46,7 +46,7 @@ export async function loadGetInitialProps (Component, ctx) { if (!Component.getInitialProps) return {} const props = await Component.getInitialProps(ctx) - if (!props) { + if (!props && (!ctx.res || !ctx.res.finished)) { const compName = Component.displayName || Component.name const message = `"${compName}.getInitialProps()" should resolve to an object. But found "${props}" instead.` throw new Error(message) diff --git a/server/render.js b/server/render.js index 901b21b8b12b0..edee8c1149582 100644 --- a/server/render.js +++ b/server/render.js @@ -83,6 +83,8 @@ async function doRender (req, res, pathname, query, { const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage }) + if (res.finished) return + const doc = createElement(Document, { __NEXT_DATA__: { component,