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,13 +2,30 @@ 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
|
return <html>
|
||||||
if (!staticMarkup) {
|
<head>
|
||||||
|
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
||||||
|
<style dangerouslySetInnerHTML={{ __html: css }} />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
||||||
|
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ __html: '__NEXT_DATA__ = ' + htmlescape(data) }} />}
|
||||||
|
{staticMarkup ? null : createClientScript({ dev, cdn })}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
}
|
||||||
|
|
||||||
|
function createClientScript ({ dev, cdn }) {
|
||||||
if (dev) {
|
if (dev) {
|
||||||
script = <script type='text/javascript' src='/_next/next-dev.bundle.js' />
|
return <script type='text/javascript' src='/_next/next-dev.bundle.js' />
|
||||||
} else {
|
}
|
||||||
script = <script dangerouslySetInnerHTML={{ __html: `
|
|
||||||
|
if (!cdn) {
|
||||||
|
return <script type='text/javascript' src='/_next/next.bundle.js' />
|
||||||
|
}
|
||||||
|
|
||||||
|
return <script dangerouslySetInnerHTML={{ __html: `
|
||||||
(function () {
|
(function () {
|
||||||
load('https://cdn.zeit.co/next.js/${pkg.version}/next.min.js', function (err) {
|
load('https://cdn.zeit.co/next.js/${pkg.version}/next.min.js', function (err) {
|
||||||
if (err) load('/_next/next.bundle.js')
|
if (err) load('/_next/next.bundle.js')
|
||||||
|
@ -25,18 +42,4 @@ export default ({ head, css, html, data, dev, staticMarkup }) => {
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
`}} />
|
`}} />
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return <html>
|
|
||||||
<head>
|
|
||||||
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
|
||||||
<style dangerouslySetInnerHTML={{ __html: css }} />
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
|
|
||||||
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ __html: '__NEXT_DATA__ = ' + htmlescape(data) }} />}
|
|
||||||
{script}
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
}
|
}
|
||||||
|
|
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