Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
93d85670
Commit
93d85670
authored
Oct 28, 2016
by
Naoyuki Kanezawa
Committed by
GitHub
Oct 28, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add cdn config (#111)
parent
da1cdcb8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
30 deletions
+68
-30
document.js
lib/document.js
+30
-27
config.js
server/config.js
+33
-0
render.js
server/render.js
+5
-3
No files found.
lib/document.js
View file @
93d85670
...
@@ -2,32 +2,7 @@ import React from 'react'
...
@@ -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 }) => {
...
@@ -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
)
}}
/>
}
{
s
cript
}
{
s
taticMarkup
?
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)
}
})()
`
}}
/
>
}
server/config.js
0 → 100644
View file @
93d85670
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
)
}
server/render.js
View file @
93d85670
...
@@ -2,13 +2,14 @@ import { join } from 'path'
...
@@ -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 = {}, {
...
@@ -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 = {}, {
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment