8000 add cdn config (#111) · JavaScriptExpert/next.js@93d8567 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93d8567

Browse files
authored
add cdn config (vercel#111)
1 parent da1cdcb commit 93d8567

File tree

3 files changed

+68
-30
lines changed

3 files changed

+68
-30
lines changed

lib/document.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,7 @@ import React from 'react'
22
import htmlescape from 'htmlescape'
33
import pkg from '../../package.json'
44

5-
export default ({ head, css, html, data, dev, staticMarkup }) => {
6-
let script
7-
if (!staticMarkup) {
8-
if (dev) {
9-
script = <script type='text/javascript' src='/_next/next-dev.bundle.js' />
10-
} else {
11-
script = <script dangerouslySetInnerHTML={{ __html: `
12-
(function () {
13-
load('https://cdn.zeit.co/next.js/${pkg.version}/next.min.js', function (err) {
14-
if (err) load('/_next/next.bundle.js')
15-
})
16-
17-
function load (src, fn) {
18-
fn = fn || function () {}
19-
var script = document.createElement('script')
20-
script.src = src
21-
script.onload = function () { fn(null) }
22-
script.onerror = fn
23-
script.crossorigin = 'anonymous'
24-
document.head.appendChild(script)
25-
}
26-
})()
27-
`}} />
28-
}
29-
}
30-
5+
export default ({ head, css, html, data, dev, staticMarkup, cdn }) => {
316
return <html>
327
<head>
338
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
@@ -36,7 +11,35 @@ export default ({ head, css, html, data, dev, staticMarkup }) => {
3611
<body>
3712
<div id='__next' dangerouslySetInnerHTML={{ __html: html }} />
3813
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ __html: '__NEXT_DATA__ = ' + htmlescape(data) }} />}
39-
{script}
14+
{staticMarkup ? null : createClientScript({ dev, cdn })}
4015
</body>
4116
</html>
4217
}
18+
19+
function createClientScript ({ dev, cdn }) {
20+
if (dev) {
21+
return <script type='text/javascript' src='/_next/next-dev.bundle.js' />
22+
}
23+
24+
if (!cdn) {
25+
return <script type='text/javascript' src='/_next/next.bundle.js' />
26+
}
27+
28+
return <script dangerouslySetInnerHTML={{ __html: `
29+
(function () {
30+
load('https://cdn.zeit.co/next.js/${pkg.version}/next.min.js', function (err) {
31+
if (err) load('/_next/next.bundle.js')
32+
})
33+
34+
function load (src, fn) {
35+
fn = fn || function () {}
36+
var script = document.createElement('script')
37+
script.src = src
38+
script.onload = function () { fn(null) }
39+
script.onerror = fn
40+
script.crossorigin = 'anonymous'
41+
document.head.appendChild(script)
42+
}
43+
})()
44+
`}} />
45+
}

server/config.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { join } from 'path'
2+
import { readFile } from 'mz/fs'
3+
4+
const cache = new Map()
5+
6+
const defaultConfig = { cdn: true }
7+
8+
export default function getConfig (dir) {
9+
if (!cache.has(dir)) {
10+
cache.set(dir, loadConfig(dir))
11+
}
12+
return cache.get(dir)
13+
}
14+
15+
async function loadConfig (dir) {
16+
const path = join(dir, 'package.json')
17+
18+
let data
19+
try {
20+
data = await readFile(path, 'utf8')
21+
} catch (err) {
22+
if (err.code === 'ENOENT') {
23+
data = '{}'
24+
} else {
25+
throw err
26+
}
27+
}
28+
29+
// no try-cache, it must be a valid json
30+
const config = JSON.parse(data).next || {}
31+
32+
return Object.assign({}, defaultConfig, config)
33+
}

server/render.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { join } from 'path'
22
import { parse } from 'url'
33
import { createElement } from 'react'
44
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
5+
import { renderStatic } from 'glamor/server'
56
import requireModule from './require'
67
import read from './read'
8+
import getConfig from './config'
79
import Router from '../lib/router'
810
import Document from '../lib/document'
911
import Head from '../lib/head'
1012
import App from '../lib/app'
11-
import { renderStatic } from 'glamor/server'
1213

1314
export async function render (url, ctx = {}, {
1415
dir = process.cwd(),
@@ -33,6 +34,7 @@ export async function render (url, ctx = {}, {
3334
})
3435

3536
const head = Head.rewind() || []
37+
const config = await getConfig(dir)
3638

3739
const doc = createElement(Document, {
3840
html,
@@ -44,9 +46,9 @@ export async function render (url, ctx = {}, {
4446
ids: ids,
4547
err: ctx.err ? errorToJSON(ctx.err) : null
4648
},
47-
hotReload: false,
4849
dev,
49-
staticMarkup
50+
staticMarkup,
51+
cdn: config.cdn
5052
})
5153

5254
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)

0 commit comments

Comments
 (0)
0