1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Check if headers are sent (#3642)

This commit is contained in:
Tim Neutkens 2018-01-31 14:12:21 +01:00 committed by Arunoda Susiripala
parent c52a3246ae
commit 7b2cd84fee
2 changed files with 16 additions and 6 deletions

View file

@ -46,15 +46,25 @@ export function getDisplayName (Component) {
return Component.displayName || Component.name || 'Unknown' return Component.displayName || Component.name || 'Unknown'
} }
export function isResSent (res) {
return res.finished || res.headersSent
}
export async function loadGetInitialProps (Component, ctx) { export async function loadGetInitialProps (Component, ctx) {
if (!Component.getInitialProps) return {} if (!Component.getInitialProps) return {}
const props = await Component.getInitialProps(ctx) const props = await Component.getInitialProps(ctx)
if (!props && (!ctx.res || !ctx.res.finished)) {
if (ctx.res && isResSent(ctx.res)) {
return props
}
if (!props) {
const compName = getDisplayName(Component) const compName = getDisplayName(Component)
const message = `"${compName}.getInitialProps()" should resolve to an object. But found "${props}" instead.` const message = `"${compName}.getInitialProps()" should resolve to an object. But found "${props}" instead.`
throw new Error(message) throw new Error(message)
} }
return props return props
} }

View file

@ -7,7 +7,7 @@ import fresh from 'fresh'
import requireModule from './require' import requireModule from './require'
import getConfig from './config' import getConfig from './config'
import { Router } from '../lib/router' import { Router } from '../lib/router'
import { loadGetInitialProps } from '../lib/utils' import { loadGetInitialProps, isResSent } from '../lib/utils'
import { getAvailableChunks } from './utils' import { getAvailableChunks } from './utils'
import Head, { defaultHead } from '../lib/head' import Head, { defaultHead } from '../lib/head'
import App from '../lib/app' import App from '../lib/app'
@ -66,7 +66,7 @@ async function doRender (req, res, pathname, query, {
const props = await loadGetInitialProps(Component, ctx) const props = await loadGetInitialProps(Component, ctx)
// the response might be finshed on the getinitialprops call // the response might be finshed on the getinitialprops call
if (res.finished) return if (isResSent(res)) return
const renderPage = (enhancer = Page => Page) => { const renderPage = (enhancer = Page => Page) => {
const app = createElement(App, { const app = createElement(App, {
@ -99,7 +99,7 @@ async function doRender (req, res, pathname, query, {
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage }) const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
if (res.finished) return if (isResSent(res)) return
if (!Document.prototype || !Document.prototype.isReactComponent) throw new Error('_document.js is not exporting a React element') if (!Document.prototype || !Document.prototype.isReactComponent) throw new Error('_document.js is not exporting a React element')
const doc = createElement(Document, { const doc = createElement(Document, {
@ -155,7 +155,7 @@ export async function renderScriptError (req, res, page, error, customFields, {
} }
export function sendHTML (req, res, html, method, { dev }) { export function sendHTML (req, res, html, method, { dev }) {
if (res.finished) return if (isResSent(res)) return
const etag = generateETag(html) const etag = generateETag(html)
if (fresh(req.headers, { etag })) { if (fresh(req.headers, { etag })) {
@ -179,7 +179,7 @@ export function sendHTML (req, res, html, method, { dev }) {
} }
export function sendJSON (res, obj, method) { export function sendJSON (res, obj, method) {
if (res.finished) return if (isResSent(res)) return
const json = JSON.stringify(obj) const json = JSON.stringify(obj)
res.setHeader('Content-Type', 'application/json') res.setHeader('Content-Type', 'application/json')