2017-04-10 18:35:26 +00:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2016-12-16 18:42:40 +00:00
|
|
|
import htmlescape from 'htmlescape'
|
2016-12-19 18:42:19 +00:00
|
|
|
import flush from 'styled-jsx/server'
|
2016-12-16 18:42:40 +00:00
|
|
|
|
|
|
|
export default class Document extends Component {
|
|
|
|
static getInitialProps ({ renderPage }) {
|
2017-04-01 21:03:40 +00:00
|
|
|
const { html, head, errorHtml } = renderPage()
|
2017-02-26 12:28:00 +00:00
|
|
|
const styles = flush()
|
2017-04-01 21:03:40 +00:00
|
|
|
return { html, head, errorHtml, styles }
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static childContextTypes = {
|
|
|
|
_documentProps: PropTypes.any
|
|
|
|
}
|
|
|
|
|
|
|
|
getChildContext () {
|
|
|
|
return { _documentProps: this.props }
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return <html>
|
|
|
|
<Head />
|
|
|
|
<body>
|
|
|
|
<Main />
|
|
|
|
<NextScript />
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Head extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
_documentProps: PropTypes.any
|
|
|
|
}
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
getChunkPreloadLink (filename) {
|
|
|
|
const { __NEXT_DATA__ } = this.context._documentProps
|
|
|
|
let { buildStats, assetPrefix } = __NEXT_DATA__
|
|
|
|
const hash = buildStats ? buildStats[filename].hash : '-'
|
|
|
|
|
|
|
|
return (
|
|
|
|
<link
|
|
|
|
key={filename}
|
|
|
|
rel='preload'
|
|
|
|
href={`${assetPrefix}/_next/${hash}/${filename}`}
|
|
|
|
as='script'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
getPreloadMainLinks () {
|
|
|
|
const { dev } = this.context._documentProps
|
|
|
|
if (dev) {
|
|
|
|
return [
|
|
|
|
this.getChunkPreloadLink('manifest.js'),
|
|
|
|
this.getChunkPreloadLink('commons.js'),
|
|
|
|
this.getChunkPreloadLink('main.js')
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the production mode, we have a single asset with all the JS content.
|
|
|
|
return [
|
|
|
|
this.getChunkPreloadLink('app.js')
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
render () {
|
2017-04-18 04:18:43 +00:00
|
|
|
const { head, styles, __NEXT_DATA__ } = this.context._documentProps
|
|
|
|
const { pathname, buildId, assetPrefix } = __NEXT_DATA__
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
return <head>
|
2017-04-18 04:18:43 +00:00
|
|
|
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${pathname}`} as='script' />
|
|
|
|
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error`} as='script' />
|
|
|
|
{this.getPreloadMainLinks()}
|
2016-12-16 18:42:40 +00:00
|
|
|
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
2017-02-26 12:28:00 +00:00
|
|
|
{styles || null}
|
2016-12-16 18:42:40 +00:00
|
|
|
{this.props.children}
|
|
|
|
</head>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Main extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
_documentProps: PropTypes.any
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2017-04-01 21:03:40 +00:00
|
|
|
const { html, errorHtml } = this.context._documentProps
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
|
|
|
<div id='__next-error' dangerouslySetInnerHTML={{ __html: errorHtml }} />
|
|
|
|
</div>
|
|
|
|
)
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NextScript extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
_documentProps: PropTypes.any
|
|
|
|
}
|
|
|
|
|
2017-03-24 07:51:34 +00:00
|
|
|
getChunkScript (filename, additionalProps = {}) {
|
2017-03-07 18:43:56 +00:00
|
|
|
const { __NEXT_DATA__ } = this.context._documentProps
|
2017-04-18 04:18:43 +00:00
|
|
|
let { buildStats, assetPrefix } = __NEXT_DATA__
|
2017-03-07 18:43:56 +00:00
|
|
|
const hash = buildStats ? buildStats[filename].hash : '-'
|
|
|
|
|
|
|
|
return (
|
2017-03-24 07:51:34 +00:00
|
|
|
<script
|
2017-05-03 22:03:39 +00:00
|
|
|
key={filename}
|
2017-03-24 07:51:34 +00:00
|
|
|
type='text/javascript'
|
2017-04-18 04:18:43 +00:00
|
|
|
src={`${assetPrefix}/_next/${hash}/${filename}`}
|
2017-03-24 07:51:34 +00:00
|
|
|
{...additionalProps}
|
|
|
|
/>
|
2017-03-07 18:43:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-03-24 07:51:34 +00:00
|
|
|
getScripts () {
|
|
|
|
const { dev } = this.context._documentProps
|
|
|
|
if (dev) {
|
2017-05-03 22:03:39 +00:00
|
|
|
return [
|
|
|
|
this.getChunkScript('manifest.js'),
|
|
|
|
this.getChunkScript('commons.js'),
|
|
|
|
this.getChunkScript('main.js')
|
|
|
|
]
|
2017-03-24 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In the production mode, we have a single asset with all the JS content.
|
|
|
|
// So, we can load the script with async
|
2017-05-03 22:03:39 +00:00
|
|
|
return [this.getChunkScript('app.js', { async: true })]
|
2017-03-24 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
render () {
|
2017-01-11 20:16:18 +00:00
|
|
|
const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps
|
2017-04-18 04:18:43 +00:00
|
|
|
const { pathname, buildId, assetPrefix } = __NEXT_DATA__
|
2016-12-17 08:49:10 +00:00
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
return <div>
|
2017-01-20 00:14:05 +00:00
|
|
|
{staticMarkup ? null : <script dangerouslySetInnerHTML={{
|
2017-04-18 04:18:43 +00:00
|
|
|
__html: `
|
|
|
|
__NEXT_DATA__ = ${htmlescape(__NEXT_DATA__)}
|
|
|
|
module={}
|
|
|
|
__NEXT_LOADED_PAGES__ = []
|
|
|
|
|
|
|
|
__NEXT_REGISTER_PAGE = function (route, fn) {
|
|
|
|
__NEXT_LOADED_PAGES__.push({ route: route, fn: fn })
|
|
|
|
}
|
|
|
|
`
|
2017-01-20 00:14:05 +00:00
|
|
|
}} />}
|
2017-04-18 04:18:43 +00:00
|
|
|
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${pathname}`} />
|
|
|
|
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error`} />
|
2017-03-24 07:51:34 +00:00
|
|
|
{staticMarkup ? null : this.getScripts()}
|
2016-12-16 18:42:40 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|