mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
992ea2e875
* Add support for custom App and Component enhancers * Add ctx.renderPage test * Add tests for single enhancer function * Cleanup renderPage options check * Cleanup * Add comment about backwards compatibility for renderPage * Add more test cases
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
import Document, { Head, Main, NextScript } from 'next/document'
|
|
|
|
export default class MyDocument extends Document {
|
|
static async getInitialProps (ctx) {
|
|
let options
|
|
|
|
const enhanceComponent = Component => (props) => <div><span id='render-page-enhance-component'>RENDERED</span><Component {...props} /></div>
|
|
const enhanceApp = Component => (props) => <div><span id='render-page-enhance-app'>RENDERED</span><Component {...props} /></div>
|
|
|
|
if (ctx.query.withEnhancer) {
|
|
options = enhanceComponent
|
|
} else if (ctx.query.withEnhanceComponent || ctx.query.withEnhanceApp) {
|
|
options = {}
|
|
if (ctx.query.withEnhanceComponent) {
|
|
options.enhanceComponent = enhanceComponent
|
|
}
|
|
if (ctx.query.withEnhanceApp) {
|
|
options.enhanceApp = enhanceApp
|
|
}
|
|
}
|
|
|
|
const result = ctx.renderPage(options)
|
|
|
|
return { ...result, customProperty: 'Hello Document' }
|
|
}
|
|
|
|
render () {
|
|
return (
|
|
<html>
|
|
<Head nonce='test-nonce'>
|
|
<style>{`body { margin: 0 } /* custom! */`}</style>
|
|
</Head>
|
|
<body className='custom_class'>
|
|
<p id='custom-property'>{this.props.customProperty}</p>
|
|
<p id='document-hmr'>Hello Document HMR</p>
|
|
<Main />
|
|
<NextScript nonce='test-nonce' />
|
|
</body>
|
|
</html>
|
|
)
|
|
}
|
|
}
|