mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
add cdn config (#111)
This commit is contained in:
parent
da1cdcb843
commit
93d856706b
|
@ -2,32 +2,7 @@ import React from 'react'
|
||||||
import htmlescape from 'htmlescape'
|
import htmlescape from 'htmlescape'
|
||||||
import pkg from '../../package.json'
|
import pkg from '../../package.json'
|
||||||
|
|
||||||
export default ({ head, css, html, data, dev, staticMarkup }) => {
|
export default ({ head, css, html, data, dev, staticMarkup, cdn }) => {
|
||||||
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>
|
return <html>
|
||||||
<head>
|
<head>
|
||||||
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
||||||
|
@ -36,7 +11,35 @@ export default ({ head, css, html, data, dev, staticMarkup }) => {
|
||||||
<body>
|
<body>
|
||||||
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
||||||
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ __html: '__NEXT_DATA__ = ' + htmlescape(data) }} />}
|
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ __html: '__NEXT_DATA__ = ' + htmlescape(data) }} />}
|
||||||
{script}
|
{staticMarkup ? null : createClientScript({ dev, cdn })}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.head.appendChild(script)
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
`}} />
|
||||||
|
}
|
||||||
|
|
33
server/config.js
Normal file
33
server/config.js
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import { join } from 'path'
|
||||||
|
import { readFile } from 'mz/fs'
|
||||||
|
|
||||||
|
const cache = new Map()
|
||||||
|
|
||||||
|
const defaultConfig = { cdn: true }
|
||||||
|
|
||||||
|
export default function getConfig (dir) {
|
||||||
|
if (!cache.has(dir)) {
|
||||||
|
cache.set(dir, loadConfig(dir))
|
||||||
|
}
|
||||||
|
return cache.get(dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadConfig (dir) {
|
||||||
|
const path = join(dir, 'package.json')
|
||||||
|
|
||||||
|
let data
|
||||||
|
try {
|
||||||
|
data = await readFile(path, 'utf8')
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === 'ENOENT') {
|
||||||
|
data = '{}'
|
||||||
|
} else {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// no try-cache, it must be a valid json
|
||||||
|
const config = JSON.parse(data).next || {}
|
||||||
|
|
||||||
|
return Object.assign({}, defaultConfig, config)
|
||||||
|
}
|
|
@ -2,13 +2,14 @@ import { join } from 'path'
|
||||||
import { parse } from 'url'
|
import { parse } from 'url'
|
||||||
import { createElement } from 'react'
|
import { createElement } from 'react'
|
||||||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
||||||
|
import { renderStatic } from 'glamor/server'
|
||||||
import requireModule from './require'
|
import requireModule from './require'
|
||||||
import read from './read'
|
import read from './read'
|
||||||
|
import getConfig from './config'
|
||||||
import Router from '../lib/router'
|
import Router from '../lib/router'
|
||||||
import Document from '../lib/document'
|
import Document from '../lib/document'
|
||||||
import Head from '../lib/head'
|
import Head from '../lib/head'
|
||||||
import App from '../lib/app'
|
import App from '../lib/app'
|
||||||
import { renderStatic } from 'glamor/server'
|
|
||||||
|
|
||||||
export async function render (url, ctx = {}, {
|
export async function render (url, ctx = {}, {
|
||||||
dir = process.cwd(),
|
dir = process.cwd(),
|
||||||
|
@ -33,6 +34,7 @@ export async function render (url, ctx = {}, {
|
||||||
})
|
})
|
||||||
|
|
||||||
const head = Head.rewind() || []
|
const head = Head.rewind() || []
|
||||||
|
const config = await getConfig(dir)
|
||||||
|
|
||||||
const doc = createElement(Document, {
|
const doc = createElement(Document, {
|
||||||
html,
|
html,
|
||||||
|
@ -44,9 +46,9 @@ export async function render (url, ctx = {}, {
|
||||||
ids: ids,
|
ids: ids,
|
||||||
err: ctx.err ? errorToJSON(ctx.err) : null
|
err: ctx.err ? errorToJSON(ctx.err) : null
|
||||||
},
|
},
|
||||||
hotReload: false,
|
|
||||||
dev,
|
dev,
|
||||||
staticMarkup
|
staticMarkup,
|
||||||
|
cdn: config.cdn
|
||||||
})
|
})
|
||||||
|
|
||||||
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
|
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
|
||||||
|
|
Loading…
Reference in a new issue