|
| 1 | +import React, { Component, PropTypes } from 'react' |
| 2 | +import htmlescape from 'htmlescape' |
| 3 | +import { renderStatic } from 'glamor/server' |
| 4 | +import readPkgUp from 'read-pkg-up' |
| 5 | + |
| 6 | +const { pkg } = readPkgUp.sync({ |
| 7 | + cwd: __dirname, |
| 8 | + normalize: false |
| 9 | +}) |
| 10 | + |
| 11 | +export default class Document extends Component { |
| 12 | + static getInitialProps ({ renderPage }) { |
| 13 | + let head |
| 14 | + const { html, css, ids } = renderStatic(() => { |
| 15 | + const page = renderPage() |
| 16 | + head = page.head |
| 17 | + return page.html |
| 18 | + }) |
| 19 | + const nextCSS = { css, ids } |
| 20 | + return { html, head, nextCSS } |
| 21 | + } |
| 22 | + |
| 23 | + static childContextTypes = { |
| 24 | + _documentProps: PropTypes.any |
| 25 | + } |
| 26 | + |
| 27 | + constructor (props) { |
| 28 | + super(props) |
| 29 | + const { __NEXT_DATA__, nextCSS } = props |
| 30 | + if (nextCSS) __NEXT_DATA__.ids = nextCSS.ids |
| 31 | + } |
| 32 | + |
| 33 | + getChildContext () { |
| 34 | + return { _documentProps: this.props } |
| 35 | + } |
| 36 | + |
| 37 | + render () { |
| 38 | + return <html> |
| 39 | + <Head /> |
| 40 | + <body> |
| 41 | + <Main /> |
| 42 | + <NextScript /> |
| 43 | + </body> |
| 44 | + </html> |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export class Head extends Component { |
| 49 | + static contextTypes = { |
| 50 | + _documentProps: PropTypes.any |
| 51 | + } |
| 52 | + |
| 53 | + render () { |
| 54 | + const { head, nextCSS } = this.context._documentProps |
| 55 | + return <head> |
| 56 | + {(head || []).map((h, i) => React.cloneElement(h, { key: i }))} |
| 57 | + {nextCSS ? <style dangerouslySetInnerHTML={{ __html: nextCSS.css }} /> : null} |
| 58 | + {this.props.children} |
| 59 | + </head> |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export class Main extends Component { |
| 64 | + static contextTypes = { |
| 65 | + _documentProps: PropTypes.any |
| 66 | + } |
| 67 | + |
| 68 | + render () { |
| 69 | + const { html, __NEXT_DATA__, staticMarkup } = this.context._documentProps |
| 70 | + return <div> |
| 71 | + <div id='__next' dangerouslySetInnerHTML={{ __html: html }} /> |
| 72 | + {staticMarkup ? null : <script dangerouslySetInnerHTML={{ |
| 73 | + __html: `__NEXT_DATA__ = ${htmlescape(__NEXT_DATA__)}; module={};` |
| 74 | + }} />} |
| 75 | + </div> |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export class NextScript extends Component { |
| 80 | + static contextTypes = { |
| 81 | + _documentProps: PropTypes.any |
| 82 | + } |
| 83 | + |
| 84 | + render () { |
| 85 | + const { staticMarkup, dev, cdn } = this.context._documentProps |
| 86 | + return <div> |
| 87 | + {staticMarkup ? null : createClientScript({ dev, cdn })} |
| 88 | + <script type='text/javascript' src='/_next/commons.js' /> |
| 89 | + </div> |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function createClientScript ({ dev, cdn }) { |
| 94 | + if (dev) { |
| 95 | + return <script type='text/javascript' src='/_next/next-dev.bundle.js' /> |
| 96 | + } |
| 97 | + |
| 98 | + if (!cdn) { |
| 99 | + return <script type='text/javascript' src='/_next/next.bundle.js' /> |
| 100 | + } |
| 101 | + |
| 102 | + return <script dangerouslySetInnerHTML={{ __html: ` |
| 103 | + (function () { |
| 104 | + load('https://cdn.zeit.co/next.js/${pkg.version}/next.min.js', function (err) { |
| 105 | + if (err) load('/_next/next.bundle.js') |
| 106 | + }) |
| 107 | + function load (src, fn) { |
| 108 | + fn = fn || function () {} |
| 109 | + var script = document.createElement('script') |
| 110 | + script.src = src |
| 111 | + script.onload = function () { fn(null) } |
| 112 | + script.onerror = fn |
| 113 | + script.crossorigin = 'anonymous' |
| 114 | + document.body.appendChild(script) |
| 115 | + } |
| 116 | + })() |
| 117 | + `}} /> |
| 118 | +} |
0 commit comments