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 } < / d i v >
2017-11-23 21:12:52 +00:00
}
2016-12-16 18:42:40 +00:00
export default class Document extends Component {
2018-08-07 03:53:06 +00:00
static childContextTypes = {
_documentProps : PropTypes . any
}
2016-12-16 18:42:40 +00:00
static getInitialProps ( { renderPage } ) {
2018-09-05 20:45:17 +00:00
const { html , head , buildManifest } = renderPage ( )
2017-02-26 12:28:00 +00:00
const styles = flush ( )
2018-09-05 20:45:17 +00:00
return { html , head , styles , buildManifest }
2016-12-16 18:42:40 +00:00
}
getChildContext ( ) {
return { _documentProps : this . props }
}
render ( ) {
return < html >
< Head / >
< body >
< Main / >
< NextScript / >
< / b o d y >
< / h t m l >
}
}
export class Head extends Component {
static contextTypes = {
_documentProps : PropTypes . any
}
2018-06-28 18:16:30 +00:00
static propTypes = {
nonce : PropTypes . string
}
2018-08-07 03:53:06 +00:00
getCssLinks ( ) {
2018-07-24 09:24:40 +00:00
const { assetPrefix , files } = this . context . _documentProps
if ( ! files || files . length === 0 ) {
return null
}
2018-07-30 13:48:02 +00:00
return files . map ( ( file ) => {
2018-08-07 03:53:06 +00:00
// Only render .css files here
if ( ! /\.css$/ . exec ( file ) ) {
2018-07-30 13:48:02 +00:00
return null
}
return < link
2018-07-24 09:24:40 +00:00
key = { file }
2018-06-28 18:16:30 +00:00
nonce = { this . props . nonce }
2018-08-07 03:53:06 +00:00
rel = 'stylesheet'
2018-04-12 07:47:42 +00:00
href = { ` ${ assetPrefix } /_next/ ${ file } ` }
2018-08-07 03:53:06 +00:00
/ >
} )
}
getPreloadDynamicChunks ( ) {
const { dynamicImports , assetPrefix } = this . context . _documentProps
return dynamicImports . map ( ( bundle ) => {
2018-08-20 12:19:36 +00:00
return < link
rel = 'preload'
2018-08-07 03:53:06 +00:00
key = { bundle . file }
2018-08-28 15:56:48 +00:00
href = { ` ${ assetPrefix } /_next/ ${ bundle . file } ` }
2017-04-12 10:41:54 +00:00
as = 'script'
2018-08-07 03:53:06 +00:00
nonce = { this . props . nonce }
2017-04-12 10:41:54 +00:00
/ >
2018-07-30 13:48:02 +00:00
} )
}
2018-08-07 03:53:06 +00:00
getPreloadMainLinks ( ) {
2018-07-30 13:48:02 +00:00
const { assetPrefix , files } = this . context . _documentProps
if ( ! files || files . length === 0 ) {
return null
}
return files . map ( ( file ) => {
2018-08-07 03:53:06 +00:00
// Only render .js files here
if ( ! /\.js$/ . exec ( file ) ) {
2018-07-30 13:48:02 +00:00
return null
}
return < link
key = { file }
nonce = { this . props . nonce }
2018-08-07 03:53:06 +00:00
rel = 'preload'
2018-07-30 13:48:02 +00:00
href = { ` ${ assetPrefix } /_next/ ${ file } ` }
2017-04-17 20:15:50 +00:00
as = 'script'
/ >
2018-07-24 09:24:40 +00:00
} )
2017-04-17 20:15:50 +00:00
}
2016-12-16 18:42:40 +00:00
render ( ) {
2018-07-24 09:24:40 +00:00
const { head , styles , assetPrefix , _ _NEXT _DATA _ _ } = this . context . _documentProps
const { page , pathname , buildId } = _ _NEXT _DATA _ _
2017-12-04 23:19:53 +00:00
const pagePathname = getPagePathname ( pathname )
2017-04-12 10:41:54 +00:00
2018-09-27 22:24:12 +00:00
let children = this . props . children
// show a warning if Head contains <title> (only in development)
if ( process . env . NODE _ENV !== 'production' ) {
children = React . Children . map ( children , ( child ) => {
if ( child && child . type === 'title' ) {
console . warn ( "Warning: <title> should not be used in _document.js's <Head>. https://err.sh/next.js/no-document-title" )
}
return child
} )
}
2017-06-08 17:27:54 +00:00
return < head { ... this . props } >
2018-09-29 04:52:36 +00:00
{ head }
2018-07-25 11:45:42 +00:00
{ page !== '/_error' && < link rel = 'preload' href = { ` ${ assetPrefix } /_next/static/ ${ buildId } /pages ${ pagePathname } ` } as = 'script' nonce = { this . props . nonce } / > }
< link rel = 'preload' href = { ` ${ assetPrefix } /_next/static/ ${ buildId } /pages/_app.js ` } as = 'script' nonce = { this . props . nonce } / >
< link rel = 'preload' href = { ` ${ assetPrefix } /_next/static/ ${ buildId } /pages/_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 ( ) }
2018-07-30 13:48:02 +00:00
{ this . getCssLinks ( ) }
2017-02-26 12:28:00 +00:00
{ styles || null }
2018-09-27 22:24:12 +00:00
{ children }
2016-12-16 18:42:40 +00:00
< / h e a d >
}
}
export class Main extends Component {
static contextTypes = {
_documentProps : PropTypes . any
}
render ( ) {
2018-09-05 20:45:17 +00:00
const { html } = this . context . _documentProps
2017-04-01 21:03:40 +00:00
return (
2018-09-20 09:38:33 +00:00
< div id = '__next' dangerouslySetInnerHTML = { { _ _html : html } } / >
2017-04-01 21:03:40 +00:00
)
2016-12-16 18:42:40 +00:00
}
}
export class NextScript extends Component {
2018-08-07 03:53:06 +00:00
static contextTypes = {
_documentProps : PropTypes . any
}
2017-10-19 19:30:34 +00:00
static propTypes = {
nonce : PropTypes . string
}
2018-08-07 03:53:06 +00:00
getDynamicChunks ( ) {
const { dynamicImports , assetPrefix } = this . context . _documentProps
return dynamicImports . map ( ( bundle ) => {
return < script
async
key = { bundle . file }
src = { ` ${ assetPrefix } /_next/ ${ bundle . file } ` }
nonce = { this . props . nonce }
/ >
} )
2016-12-16 18:42:40 +00:00
}
2018-07-24 09:24:40 +00:00
getScripts ( ) {
const { assetPrefix , files } = this . context . _documentProps
if ( ! files || files . length === 0 ) {
return null
}
2018-07-30 13:48:02 +00:00
return files . map ( ( file ) => {
// Only render .js files here
if ( ! /\.js$/ . exec ( file ) ) {
return null
}
return < script
2018-07-24 09:24:40 +00:00
key = { file }
2018-04-12 07:47:42 +00:00
src = { ` ${ assetPrefix } /_next/ ${ file } ` }
2018-06-28 18:16:30 +00:00
nonce = { this . props . nonce }
2018-07-24 09:24:40 +00:00
async
2017-03-24 07:51:34 +00:00
/ >
2018-07-30 13:48:02 +00:00
} )
2017-03-07 18:43:56 +00:00
}
2018-08-14 18:05:25 +00:00
static getInlineScriptSource ( documentProps ) {
const { _ _NEXT _DATA _ _ } = documentProps
const { page , pathname } = _ _NEXT _DATA _ _
2018-09-17 11:19:19 +00:00
return ` __NEXT_DATA__ = ${ htmlescape ( _ _NEXT _DATA _ _ ) } ;__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])} ${ page === '/_error' ? ` ;__NEXT_REGISTER_PAGE( ${ htmlescape ( pathname ) } ,function(){var e = new Error('Page does not exist: ${ htmlescape ( pathname ) } ');e.statusCode=404;return {error:e}}) ` : '' } `
2018-08-14 18:05:25 +00:00
}
2016-12-16 18:42:40 +00:00
render ( ) {
2018-08-15 19:42:56 +00:00
const { staticMarkup , assetPrefix , devFiles , _ _NEXT _DATA _ _ } = this . context . _documentProps
2018-07-24 09:24:40 +00:00
const { page , pathname , buildId } = _ _NEXT _DATA _ _
2017-12-04 23:19:53 +00:00
const pagePathname = getPagePathname ( pathname )
2016-12-17 08:49:10 +00:00
2017-11-23 21:12:52 +00:00
return < Fragment >
2018-08-17 18:28:48 +00:00
{ devFiles ? devFiles . map ( ( file ) => < script key = { file } src = { ` ${ assetPrefix } /_next/ ${ file } ` } nonce = { this . props . nonce } / > ) : null }
2017-10-19 19:30:34 +00:00
{ staticMarkup ? null : < script nonce = { this . props . nonce } dangerouslySetInnerHTML = { {
2018-08-14 18:05:25 +00:00
_ _html : NextScript . getInlineScriptSource ( this . context . _documentProps )
2017-01-20 00:14:05 +00:00
} } / > }
2018-07-25 11:45:42 +00:00
{ page !== '/_error' && < script async id = { ` __NEXT_PAGE__ ${ pathname } ` } src = { ` ${ assetPrefix } /_next/static/ ${ buildId } /pages ${ pagePathname } ` } nonce = { this . props . nonce } / > }
< script async id = { ` __NEXT_PAGE__/_app ` } src = { ` ${ assetPrefix } /_next/static/ ${ buildId } /pages/_app.js ` } nonce = { this . props . nonce } / >
< script async id = { ` __NEXT_PAGE__/_error ` } src = { ` ${ assetPrefix } /_next/static/ ${ buildId } /pages/_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
< / F r a g m e n t >
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
}