mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Load client js from cdn with fallback (#34)
* load client js from cdn with fallback * fix cdn url
This commit is contained in:
parent
08811c7c95
commit
ebecbcf89e
|
@ -1,7 +1,33 @@
|
|||
import React from 'react'
|
||||
import htmlescape from 'htmlescape'
|
||||
import pkg from '../../package.json'
|
||||
|
||||
export default ({ head, css, html, data, dev, staticMarkup }) => {
|
||||
let script
|
||||
if (!staticMarkup) {
|
||||
if (dev) {
|
||||
script = <script type='text/javascript' src='/_next/next-dev.bundle.js' />
|
||||
} else {
|
||||
script = <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.head.appendChild(script)
|
||||
}
|
||||
})()
|
||||
`}} />
|
||||
}
|
||||
}
|
||||
|
||||
return <html>
|
||||
<head>
|
||||
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
||||
|
@ -10,7 +36,7 @@ export default ({ head, css, html, data, dev, staticMarkup }) => {
|
|||
<body>
|
||||
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
||||
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ __html: '__NEXT_DATA__ = ' + htmlescape(data) }} />}
|
||||
{staticMarkup ? null : <script type='text/javascript' src={dev ? '/_next/next-dev.bundle.js' : '/_next/next.bundle.js'} />}
|
||||
{script}
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue