mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
0b3db91731
* allows to not return props if response is already finished on getInitialProps * check res.finished after getInitialProps call of Document
56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
export function warn (message) {
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
console.error(message)
|
|
}
|
|
}
|
|
|
|
export function execOnce (fn) {
|
|
let used = false
|
|
return (...args) => {
|
|
if (!used) {
|
|
used = true
|
|
fn.apply(this, args)
|
|
}
|
|
}
|
|
}
|
|
|
|
export function deprecated (fn, message) {
|
|
if (process.env.NODE_ENV === 'production') return fn
|
|
|
|
let warned = false
|
|
const newFn = function (...args) {
|
|
if (!warned) {
|
|
warned = true
|
|
console.error(message)
|
|
}
|
|
return fn.apply(this, args)
|
|
}
|
|
|
|
// copy all properties
|
|
Object.assign(newFn, fn)
|
|
|
|
return newFn
|
|
}
|
|
|
|
export function printAndExit (message, code = 1) {
|
|
if (code === 0) {
|
|
console.log(message)
|
|
} else {
|
|
console.error(message)
|
|
}
|
|
|
|
process.exit(code)
|
|
}
|
|
|
|
export async function loadGetInitialProps (Component, ctx) {
|
|
if (!Component.getInitialProps) return {}
|
|
|
|
const props = await Component.getInitialProps(ctx)
|
|
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)
|
|
}
|
|
return props
|
|
}
|