1
0
Fork 0
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:
Naoyuki Kanezawa 2016-10-28 23:38:24 +09:00 committed by GitHub
parent da1cdcb843
commit 93d856706b
3 changed files with 68 additions and 30 deletions

View file

@ -2,32 +2,7 @@ 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)
}
})()
`}} />
}
}
export default ({ head, css, html, data, dev, staticMarkup, cdn }) => {
return <html>
<head>
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
@ -36,7 +11,35 @@ export default ({ head, css, html, data, dev, staticMarkup }) => {
<body>
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ __html: '__NEXT_DATA__ = ' + htmlescape(data) }} />}
{script}
{staticMarkup ? null : createClientScript({ dev, cdn })}
</body>
</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
View 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)
}

View file

@ -2,13 +2,14 @@ import { join } from 'path'
import { parse } from 'url'
import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import { renderStatic } from 'glamor/server'
import requireModule from './require'
import read from './read'
import getConfig from './config'
import Router from '../lib/router'
import Document from '../lib/document'
import Head from '../lib/head'
import App from '../lib/app'
import { renderStatic } from 'glamor/server'
export async function render (url, ctx = {}, {
dir = process.cwd(),
@ -33,6 +34,7 @@ export async function render (url, ctx = {}, {
})
const head = Head.rewind() || []
const config = await getConfig(dir)
const doc = createElement(Document, {
html,
@ -44,9 +46,9 @@ export async function render (url, ctx = {}, {
ids: ids,
err: ctx.err ? errorToJSON(ctx.err) : null
},
hotReload: false,
dev,
staticMarkup
staticMarkup,
cdn: config.cdn
})
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)