2018-04-03 07:34:07 +00:00
|
|
|
/* eslint-disable */
|
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
|
|
|
|
2017-12-02 17:00:40 +00:00
|
|
|
const Fragment = React.Fragment || function Fragment ({ children }) {
|
2018-01-30 15:40:52 +00:00
|
|
|
return <div>{children}</div>
|
2017-11-23 21:12:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
export default class Document extends Component {
|
|
|
|
static getInitialProps ({ renderPage }) {
|
2018-04-12 07:47:42 +00:00
|
|
|
const { html, head, errorHtml, chunks, buildManifest } = renderPage()
|
2017-02-26 12:28:00 +00:00
|
|
|
const styles = flush()
|
2018-04-12 07:47:42 +00:00
|
|
|
return { html, head, errorHtml, chunks, styles, buildManifest }
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-28 18:16:30 +00:00
|
|
|
static propTypes = {
|
|
|
|
nonce: PropTypes.string
|
|
|
|
}
|
|
|
|
|
2017-04-12 10:41:54 +00:00
|
|
|
getChunkPreloadLink (filename) {
|
2018-04-12 07:47:42 +00:00
|
|
|
const { __NEXT_DATA__, buildManifest } = this.context._documentProps
|
2018-03-06 09:45:29 +00:00
|
|
|
let { assetPrefix, buildId } = __NEXT_DATA__
|
2017-04-12 10:41:54 +00:00
|
|
|
|
2018-04-12 07:47:42 +00:00
|
|
|
const files = buildManifest[filename]
|
|
|
|
|
|
|
|
return files.map(file => {
|
|
|
|
return <link
|
2017-04-12 10:41:54 +00:00
|
|
|
key={filename}
|
2018-06-28 18:16:30 +00:00
|
|
|
nonce={this.props.nonce}
|
2017-04-12 10:41:54 +00:00
|
|
|
rel='preload'
|
2018-04-12 07:47:42 +00:00
|
|
|
href={`${assetPrefix}/_next/${file}`}
|
2017-04-12 10:41:54 +00:00
|
|
|
as='script'
|
|
|
|
/>
|
2018-04-12 07:47:42 +00:00
|
|
|
})
|
2017-04-12 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getPreloadMainLinks () {
|
|
|
|
const { dev } = this.context._documentProps
|
|
|
|
if (dev) {
|
|
|
|
return [
|
2018-04-12 07:47:42 +00:00
|
|
|
...this.getChunkPreloadLink('manifest.js'),
|
|
|
|
...this.getChunkPreloadLink('main.js')
|
2017-04-12 10:41:54 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the production mode, we have a single asset with all the JS content.
|
|
|
|
return [
|
2018-04-12 07:47:42 +00:00
|
|
|
...this.getChunkPreloadLink('main.js')
|
2017-04-12 10:41:54 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:15:50 +00:00
|
|
|
getPreloadDynamicChunks () {
|
2017-04-18 16:12:21 +00:00
|
|
|
const { chunks, __NEXT_DATA__ } = this.context._documentProps
|
2017-12-27 18:59:17 +00:00
|
|
|
let { assetPrefix } = __NEXT_DATA__
|
|
|
|
return chunks.filenames.map((chunk) => (
|
2017-04-17 20:15:50 +00:00
|
|
|
<link
|
|
|
|
key={chunk}
|
|
|
|
rel='preload'
|
2017-12-27 18:59:17 +00:00
|
|
|
href={`${assetPrefix}/_next/webpack/chunks/${chunk}`}
|
2017-04-17 20:15:50 +00:00
|
|
|
as='script'
|
2018-06-28 18:16:30 +00:00
|
|
|
nonce={this.props.nonce}
|
2017-04-17 20:15:50 +00:00
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
render () {
|
2017-04-12 10:41:54 +00:00
|
|
|
const { head, styles, __NEXT_DATA__ } = this.context._documentProps
|
2018-02-09 15:55:45 +00:00
|
|
|
const { page, pathname, buildId, assetPrefix } = __NEXT_DATA__
|
2017-12-04 23:19:53 +00:00
|
|
|
const pagePathname = getPagePathname(pathname)
|
2017-04-12 10:41:54 +00:00
|
|
|
|
2017-06-08 17:27:54 +00:00
|
|
|
return <head {...this.props}>
|
2017-12-04 16:15:30 +00:00
|
|
|
{(head || []).map((h, i) => React.cloneElement(h, { key: h.key || i }))}
|
2018-06-28 18:16:30 +00:00
|
|
|
{page !== '/_error' && <link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${pagePathname}`} as='script' nonce={this.props.nonce} />}
|
|
|
|
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_app.js`} as='script' nonce={this.props.nonce} />
|
|
|
|
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error.js`} as='script' nonce={this.props.nonce} />
|
2017-04-17 20:15:50 +00:00
|
|
|
{this.getPreloadDynamicChunks()}
|
2017-04-12 10:41:54 +00:00
|
|
|
{this.getPreloadMainLinks()}
|
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 (
|
2017-11-23 21:12:52 +00:00
|
|
|
<Fragment>
|
2017-04-01 21:03:40 +00:00
|
|
|
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
|
|
|
<div id='__next-error' dangerouslySetInnerHTML={{ __html: errorHtml }} />
|
2017-11-23 21:12:52 +00:00
|
|
|
</Fragment>
|
2017-04-01 21:03:40 +00:00
|
|
|
)
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NextScript extends Component {
|
2017-10-19 19:30:34 +00:00
|
|
|
static propTypes = {
|
|
|
|
nonce: PropTypes.string
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
static contextTypes = {
|
|
|
|
_documentProps: PropTypes.any
|
|
|
|
}
|
|
|
|
|
2017-03-24 07:51:34 +00:00
|
|
|
getChunkScript (filename, additionalProps = {}) {
|
2018-04-12 07:47:42 +00:00
|
|
|
const { __NEXT_DATA__, buildManifest } = this.context._documentProps
|
2018-03-06 09:45:29 +00:00
|
|
|
let { assetPrefix, buildId } = __NEXT_DATA__
|
2017-03-07 18:43:56 +00:00
|
|
|
|
2018-04-12 07:47:42 +00:00
|
|
|
const files = buildManifest[filename]
|
|
|
|
|
|
|
|
return files.map((file) => (
|
2017-03-24 07:51:34 +00:00
|
|
|
<script
|
2017-05-03 22:03:39 +00:00
|
|
|
key={filename}
|
2018-04-12 07:47:42 +00:00
|
|
|
src={`${assetPrefix}/_next/${file}`}
|
2018-06-28 18:16:30 +00:00
|
|
|
nonce={this.props.nonce}
|
2017-03-24 07:51:34 +00:00
|
|
|
{...additionalProps}
|
|
|
|
/>
|
2018-04-12 07:47:42 +00:00
|
|
|
))
|
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 [
|
2018-04-12 07:47:42 +00:00
|
|
|
...this.getChunkScript('manifest.js'),
|
|
|
|
...this.getChunkScript('main.js')
|
2017-05-03 22:03:39 +00:00
|
|
|
]
|
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
|
2018-04-12 07:47:42 +00:00
|
|
|
return [...this.getChunkScript('main.js', { async: true })]
|
2017-03-24 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:15:50 +00:00
|
|
|
getDynamicChunks () {
|
2017-04-18 16:12:21 +00:00
|
|
|
const { chunks, __NEXT_DATA__ } = this.context._documentProps
|
2017-12-27 18:59:17 +00:00
|
|
|
let { assetPrefix } = __NEXT_DATA__
|
2017-04-17 20:15:50 +00:00
|
|
|
return (
|
2017-11-23 21:12:52 +00:00
|
|
|
<Fragment>
|
2017-12-27 18:59:17 +00:00
|
|
|
{chunks.filenames.map((chunk) => (
|
2017-04-17 20:15:50 +00:00
|
|
|
<script
|
|
|
|
async
|
|
|
|
key={chunk}
|
2017-12-27 18:59:17 +00:00
|
|
|
src={`${assetPrefix}/_next/webpack/chunks/${chunk}`}
|
2018-06-28 18:16:30 +00:00
|
|
|
nonce={this.props.nonce}
|
2017-04-17 20:15:50 +00:00
|
|
|
/>
|
|
|
|
))}
|
2017-11-23 21:12:52 +00:00
|
|
|
</Fragment>
|
2017-04-17 20:15:50 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
render () {
|
2017-04-17 20:15:50 +00:00
|
|
|
const { staticMarkup, __NEXT_DATA__, chunks } = this.context._documentProps
|
2018-02-09 15:55:45 +00:00
|
|
|
const { page, pathname, buildId, assetPrefix } = __NEXT_DATA__
|
2017-12-04 23:19:53 +00:00
|
|
|
const pagePathname = getPagePathname(pathname)
|
2016-12-17 08:49:10 +00:00
|
|
|
|
2017-12-27 18:59:17 +00:00
|
|
|
__NEXT_DATA__.chunks = chunks.names
|
2017-04-17 20:15:50 +00:00
|
|
|
|
2017-11-23 21:12:52 +00:00
|
|
|
return <Fragment>
|
2017-10-19 19:30:34 +00:00
|
|
|
{staticMarkup ? null : <script nonce={this.props.nonce} dangerouslySetInnerHTML={{
|
2017-04-11 14:33:18 +00:00
|
|
|
__html: `
|
|
|
|
__NEXT_DATA__ = ${htmlescape(__NEXT_DATA__)}
|
|
|
|
module={}
|
|
|
|
__NEXT_LOADED_PAGES__ = []
|
2017-04-17 20:15:50 +00:00
|
|
|
__NEXT_LOADED_CHUNKS__ = []
|
2017-04-11 14:33:18 +00:00
|
|
|
|
|
|
|
__NEXT_REGISTER_PAGE = function (route, fn) {
|
|
|
|
__NEXT_LOADED_PAGES__.push({ route: route, fn: fn })
|
|
|
|
}
|
2017-04-17 20:15:50 +00:00
|
|
|
|
|
|
|
__NEXT_REGISTER_CHUNK = function (chunkName, fn) {
|
|
|
|
__NEXT_LOADED_CHUNKS__.push({ chunkName: chunkName, fn: fn })
|
|
|
|
}
|
2018-02-09 15:55:45 +00:00
|
|
|
|
|
|
|
${page === '_error' && `
|
2018-02-09 21:19:08 +00:00
|
|
|
__NEXT_REGISTER_PAGE(${htmlescape(pathname)}, function() {
|
2018-02-09 15:55:45 +00:00
|
|
|
var error = new Error('Page does not exist: ${htmlescape(pathname)}')
|
|
|
|
error.statusCode = 404
|
2018-02-13 15:13:22 +00:00
|
|
|
|
2018-02-09 15:55:45 +00:00
|
|
|
return { error: error }
|
|
|
|
})
|
|
|
|
`}
|
2017-04-11 14:33:18 +00:00
|
|
|
`
|
2017-01-20 00:14:05 +00:00
|
|
|
}} />}
|
2018-06-28 18:16:30 +00:00
|
|
|
{page !== '/_error' && <script async id={`__NEXT_PAGE__${pathname}`} src={`${assetPrefix}/_next/${buildId}/page${pagePathname}`} nonce={this.props.nonce} />}
|
|
|
|
<script async id={`__NEXT_PAGE__/_app`} src={`${assetPrefix}/_next/${buildId}/page/_app.js`} nonce={this.props.nonce} />
|
|
|
|
<script async id={`__NEXT_PAGE__/_error`} src={`${assetPrefix}/_next/${buildId}/page/_error.js`} nonce={this.props.nonce} />
|
2017-04-17 20:15:50 +00:00
|
|
|
{staticMarkup ? null : this.getDynamicChunks()}
|
2017-03-24 07:51:34 +00:00
|
|
|
{staticMarkup ? null : this.getScripts()}
|
2017-11-23 21:12:52 +00:00
|
|
|
</Fragment>
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-11 13:20:58 +00:00
|
|
|
|
2017-12-04 23:19:53 +00:00
|
|
|
function getPagePathname (pathname) {
|
|
|
|
if (pathname === '/') {
|
|
|
|
return '/index.js'
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${pathname}.js`
|
2017-05-11 13:20:58 +00:00
|
|
|
}
|