2017-03-15 14:23:42 +00:00
|
|
|
import Document, { Head, Main, NextScript } from 'next/document'
|
|
|
|
import Helmet from 'react-helmet'
|
|
|
|
|
|
|
|
export default class extends Document {
|
2017-04-03 04:21:26 +00:00
|
|
|
static async getInitialProps (...args) {
|
|
|
|
const documentProps = await super.getInitialProps(...args)
|
2017-03-15 14:23:42 +00:00
|
|
|
// see https://github.com/nfl/react-helmet#server-usage for more information
|
|
|
|
// 'head' was occupied by 'renderPage().head', we cannot use it
|
2017-07-18 06:03:21 +00:00
|
|
|
return { ...documentProps, helmet: Helmet.renderStatic() }
|
2017-03-15 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// should render on <html>
|
|
|
|
get helmetHtmlAttrComponents () {
|
|
|
|
return this.props.helmet.htmlAttributes.toComponent()
|
|
|
|
}
|
|
|
|
|
2017-07-18 06:03:21 +00:00
|
|
|
// should render on <body>
|
|
|
|
get helmetBodyAttrComponents () {
|
|
|
|
return this.props.helmet.bodyAttributes.toComponent()
|
|
|
|
}
|
|
|
|
|
2017-03-15 14:23:42 +00:00
|
|
|
// should render on <head>
|
|
|
|
get helmetHeadComponents () {
|
|
|
|
return Object.keys(this.props.helmet)
|
2018-03-27 18:11:03 +00:00
|
|
|
.filter(el => el !== 'htmlAttributes' && el !== 'bodyAttributes')
|
|
|
|
.map(el => this.props.helmet[el].toComponent())
|
2017-03-15 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get helmetJsx () {
|
2018-12-17 16:34:32 +00:00
|
|
|
return (
|
|
|
|
<Helmet
|
|
|
|
htmlAttributes={{ lang: 'en' }}
|
|
|
|
title='Hello next.js!'
|
|
|
|
meta={[
|
|
|
|
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
|
|
|
|
{ property: 'og:title', content: 'Hello next.js!' }
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
)
|
2017-03-15 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-12-17 16:34:32 +00:00
|
|
|
return (
|
|
|
|
<html {...this.helmetHtmlAttrComponents}>
|
|
|
|
<Head>
|
|
|
|
{this.helmetJsx}
|
|
|
|
{this.helmetHeadComponents}
|
|
|
|
</Head>
|
|
|
|
<body {...this.helmetBodyAttrComponents}>
|
|
|
|
<Main />
|
|
|
|
<NextScript />
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
)
|
2017-03-15 14:23:42 +00:00
|
|
|
}
|
|
|
|
}
|