Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
249 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dist/server/document') |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('next/document') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { resolve, join, dirname } from 'path' | ||
import { readFile, writeFile } from 'mz/fs' | ||
import { transform } from 'babel-core' | ||
import chokidar from 'chokidar' | ||
import mkdirp from 'mkdirp-then' | ||
|
||
const babelRuntimePath = require.resolve('babel-runtime/package') | ||
.replace(/[\\/]package\.json$/, '') | ||
|
||
export default babel | ||
|
||
async function babel (dir, { dev = false } = {}) { | ||
dir = resolve(dir) | ||
|
||
let src | ||
try { | ||
src = await readFile(join(dir, 'pages', '_document.js'), 'utf8') | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
src = await readFile(join(__dirname, '..', '..', 'pages', '_document.js'), 'utf8') | ||
} else { | ||
throw err | ||
} | ||
} | ||
|
||
const { code } = transform(src, { | ||
babelrc: false, | ||
sourceMaps: dev ? 'inline' : false, | ||
presets: ['es2015', 'react'], | ||
plugins: [ | ||
require.resolve('babel-plugin-react-require'), | ||
require.resolve('babel-plugin-transform-async-to-generator'), | ||
require.resolve('babel-plugin-transform-object-rest-spread'), | ||
require.resolve('babel-plugin-transform-class-properties'), | ||
require.resolve('babel-plugin-transform-runtime'), | ||
[ | ||
require.resolve('babel-plugin-module-resolver'), | ||
{ | ||
alias: { | ||
'babel-runtime': babelRuntimePath, | ||
react: require.resolve('react'), | ||
'next/link': require.resolve('../../lib/link'), | ||
'next/css': require.resolve('../../lib/css'), | ||
'next/head': require.resolve('../../lib/head'), | ||
'next/document': require.resolve('../../server/document') | ||
} | ||
} | ||
] | ||
] | ||
}) | ||
|
||
const file = join(dir, '.next', 'dist', 'pages', '_document.js') | ||
await mkdirp(dirname(file)) | ||
await writeFile(file, code) | ||
} | ||
|
||
export function watch (dir) { | ||
return chokidar.watch('pages/_document.js', { | ||
cwd: dir, | ||
ignoreInitial: true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React, { Component, PropTypes } from 'react' | ||
import htmlescape from 'htmlescape' | ||
import { renderStatic } from 'glamor/server' | ||
import readPkgUp from 'read-pkg-up' | ||
|
||
const { pkg } = readPkgUp.sync({ | ||
cwd: __dirname, | ||
normalize: false | ||
}) | ||
|
||
export default class Document extends Component { | ||
static getInitialProps ({ renderPage }) { | ||
let head | ||
const { html, css, ids } = renderStatic(() => { | ||
const page = renderPage() | ||
head = page.head | ||
return page.html | ||
}) | ||
const nextCSS = { css, ids } | ||
return { html, head, nextCSS } | ||
} | ||
|
||
static childContextTypes = { | ||
_documentProps: PropTypes.any | ||
} | ||
|
||
constructor (props) { | ||
super(props) | ||
const { __NEXT_DATA__, nextCSS } = props | ||
if (nextCSS) __NEXT_DATA__.ids = nextCSS.ids | ||
} | ||
|
||
getChildContext () { | ||
return { _documentProps: this.props } | ||
} | ||
|
||
render () { | ||
return <html> | ||
<Head /> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</html> | ||
} | ||
} | ||
|
||
export class Head extends Component { | ||
static contextTypes = { | ||
_documentProps: PropTypes.any | ||
} | ||
|
||
render () { | ||
const { head, nextCSS } = this.context._documentProps | ||
return <head> | ||
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))} | ||
{nextCSS ? <style dangerouslySetInnerHTML={{ __html: nextCSS.css }} /> : null} | ||
{this.props.children} | ||
</head> | ||
} | ||
} | ||
|
||
export class Main extends Component { | ||
static contextTypes = { | ||
_documentProps: PropTypes.any | ||
} | ||
|
||
render () { | ||
const { html, __NEXT_DATA__, staticMarkup } = this.context._documentProps | ||
return <div> | ||
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} /> | ||
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ | ||
__html: `__NEXT_DATA__ = ${htmlescape(__NEXT_DATA__)}; module={};` | ||
}} />} | ||
</div> | ||
} | ||
} | ||
|
||
export class NextScript extends Component { | ||
static contextTypes = { | ||
_documentProps: PropTypes.any | ||
} | ||
|
||
render () { | ||
const { staticMarkup, dev, cdn } = this.context._documentProps | ||
return <div> | ||
{staticMarkup ? null : createClientScript({ dev, cdn })} | ||
<script type='text/javascript' src='/_next/commons.js' /> | ||
</div> | ||
} | ||
} | ||
|
||
function createClientScript ({ dev, cdn }) { | ||
if (dev) { | ||
return <script type='text/javascript' src='/_next/next-dev.bundle.js' /> | ||
} | ||
|
||
if (!cdn) { | ||
return <script type='text/javascript' src='/_next/next.bundle.js' /> | ||
} | ||
|
||
return <script dangerouslySetInnerHTML={{ __html: ` | ||
(function () { | ||
load('https://cdn.zeit.co/next.js/${pkg.version}/next.min.js', function (err) { | ||
if (err) load('/_next/next.bundle.js') | ||
}) | ||
function load (src, fn) { | ||
fn = fn || function () {} | ||
var script = document.createElement('script') | ||
script.src = src | ||
script.onload = function () { fn(null) } | ||
script.onerror = fn | ||
script.crossorigin = 'anonymous' | ||
document.body.appendChild(script) | ||
} | ||
})() | ||
`}} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.