mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
![Arunoda Susiripala](/assets/img/avatar_default.png)
* Introduce script tag based page loading system. * Call ensurePage only in the dev mode. * Implement router using the page-loader. * Fix a typo and remove unwanted code. * Fix some issues related to rendering. * Fix production tests. * Fix ondemand test cases. * Fix unit tests. * Get rid of eval completely. * Remove all the inline code. * Remove the json-pages plugin. * Rename NEXT_PAGE_LOADER into __NEXT_PAGE_LOADER__ * Rename NEXT_LOADED_PAGES into __NEXT_LOADED_PAGES__ * Remove some unwanted code. * Load everything async. * Remove lib/eval-script.js We no longer need it. * Move webpack idle wait code to the page-loader. Because that's the place to do it. * Remove pageNotFound key from the error. * Remove unused error field 'buildError' * Add much better logic to normalize routes. * Get rid of mitt. * Introduce a better way to register pages. * Came back to the mitt() based page-loader. * Add link rel=preload support. * Add assetPrefix support to add support for CDNs. * Add assetPrefix support for preload links. * Update readme.md
157 lines
4.1 KiB
JavaScript
157 lines
4.1 KiB
JavaScript
import React, { Component } from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import htmlescape from 'htmlescape'
|
|
import flush from 'styled-jsx/server'
|
|
|
|
export default class Document extends Component {
|
|
static getInitialProps ({ renderPage }) {
|
|
const { html, head, errorHtml } = renderPage()
|
|
const styles = flush()
|
|
return { html, head, errorHtml, styles }
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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')
|
|
]
|
|
}
|
|
|
|
render () {
|
|
const { head, styles, __NEXT_DATA__ } = this.context._documentProps
|
|
const { pathname, buildId, assetPrefix } = __NEXT_DATA__
|
|
|
|
return <head>
|
|
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${pathname}`} as='script' />
|
|
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error`} as='script' />
|
|
{this.getPreloadMainLinks()}
|
|
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
|
{styles || null}
|
|
{this.props.children}
|
|
</head>
|
|
}
|
|
}
|
|
|
|
export class Main extends Component {
|
|
static contextTypes = {
|
|
_documentProps: PropTypes.any
|
|
}
|
|
|
|
render () {
|
|
const { html, errorHtml } = this.context._documentProps
|
|
return (
|
|
<div>
|
|
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
|
<div id='__next-error' dangerouslySetInnerHTML={{ __html: errorHtml }} />
|
|
</div>
|
|
)
|
|
}
|
|
}
|
|
|
|
export class NextScript extends Component {
|
|
static contextTypes = {
|
|
_documentProps: PropTypes.any
|
|
}
|
|
|
|
getChunkScript (filename, additionalProps = {}) {
|
|
const { __NEXT_DATA__ } = this.context._documentProps
|
|
let { buildStats, assetPrefix } = __NEXT_DATA__
|
|
const hash = buildStats ? buildStats[filename].hash : '-'
|
|
|
|
return (
|
|
<script
|
|
type='text/javascript'
|
|
src={`${assetPrefix}/_next/${hash}/${filename}`}
|
|
{...additionalProps}
|
|
/>
|
|
)
|
|
}
|
|
|
|
getScripts () {
|
|
const { dev } = this.context._documentProps
|
|
if (dev) {
|
|
return (
|
|
<div>
|
|
{ this.getChunkScript('manifest.js') }
|
|
{ this.getChunkScript('commons.js') }
|
|
{ this.getChunkScript('main.js') }
|
|
</div>
|
|
)
|
|
}
|
|
|
|
// In the production mode, we have a single asset with all the JS content.
|
|
// So, we can load the script with async
|
|
return this.getChunkScript('app.js', { async: true })
|
|
}
|
|
|
|
render () {
|
|
const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps
|
|
const { pathname, buildId, assetPrefix } = __NEXT_DATA__
|
|
|
|
return <div>
|
|
{staticMarkup ? null : <script dangerouslySetInnerHTML={{
|
|
__html: `
|
|
__NEXT_DATA__ = ${htmlescape(__NEXT_DATA__)}
|
|
module={}
|
|
__NEXT_LOADED_PAGES__ = []
|
|
|
|
__NEXT_REGISTER_PAGE = function (route, fn) {
|
|
__NEXT_LOADED_PAGES__.push({ route: route, fn: fn })
|
|
}
|
|
`
|
|
}} />}
|
|
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${pathname}`} />
|
|
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error`} />
|
|
{staticMarkup ? null : this.getScripts()}
|
|
</div>
|
|
}
|
|
}
|