Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
7997c1fd
N
next.js
项目概览
CoCo_Code_Op2
/
next.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
next.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7997c1fd
编写于
4月 11, 2017
作者:
A
Arunoda Susiripala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce a better way to register pages.
上级
fb2f90be
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
32 addition
and
49 deletion
+32
-49
client/index.js
client/index.js
+7
-5
lib/page-loader.js
lib/page-loader.js
+5
-6
server/build/plugins/pages-plugin.js
server/build/plugins/pages-plugin.js
+4
-13
server/document.js
server/document.js
+9
-1
server/render.js
server/render.js
+7
-24
未找到文件。
client/index.js
浏览文件 @
7997c1fd
...
...
@@ -28,11 +28,13 @@ const {
location
}
=
window
const
pageLoader
=
window
.
__NEXT_PAGE_LOADER__
=
new
PageLoader
(
buildId
)
if
(
window
.
__NEXT_LOADED_PAGES__
)
{
window
.
__NEXT_LOADED_PAGES__
.
forEach
((
fn
)
=>
fn
())
delete
window
.
__NEXT_LOADED_PAGES__
}
const
pageLoader
=
new
PageLoader
(
buildId
)
window
.
__NEXT_LOADED_PAGES__
.
forEach
(({
route
,
fn
})
=>
{
pageLoader
.
registerPage
(
route
,
fn
)
})
delete
window
.
__NEXT_LOADED_PAGES__
window
.
__NEXT_REGISTER_PAGE
=
pageLoader
.
registerPage
.
bind
(
pageLoader
)
const
headManager
=
new
HeadManager
()
const
appContainer
=
document
.
getElementById
(
'
__next
'
)
...
...
lib/page-loader.js
浏览文件 @
7997c1fd
...
...
@@ -71,13 +71,12 @@ export default class PageLoader {
// This method if called by the route code.
registerPage
(
route
,
regFn
)
{
const
register
=
()
=>
{
regFn
((
error
,
page
)
=>
{
route
=
this
.
normalizeRoute
(
route
)
route
=
this
.
normalizeRoute
(
route
)
// add the page to the cache
this
.
pageCache
[
route
]
=
{
error
,
page
}
})
const
register
=
()
=>
{
// add the page to the cache
const
{
error
,
page
}
=
regFn
()
this
.
pageCache
[
route
]
=
{
error
,
page
}
}
// Wait for webpack to became idle if it's not.
...
...
server/build/plugins/pages-plugin.js
浏览文件 @
7997c1fd
...
...
@@ -16,19 +16,10 @@ export default class PagesPlugin {
const
content
=
page
.
source
()
const
newContent
=
`
function loadPage () {
window.__NEXT_PAGE_LOADER__.registerPage('
${
routeName
}
', function(cb) {
var comp =
${
content
}
cb(null, comp.default)
})
}
if (window.__NEXT_PAGE_LOADER__) {
loadPage()
} else {
window.__NEXT_LOADED_PAGES__ = window.__NEXT_LOADED_PAGES__ || []
window.__NEXT_LOADED_PAGES__.push(loadPage)
}
window.__NEXT_REGISTER_PAGE('
${
routeName
}
', function() {
var comp =
${
content
}
return { page: comp.default }
})
`
// Replace the exisiting chunk with the new content
compilation
.
assets
[
chunk
.
name
]
=
{
...
...
server/document.js
浏览文件 @
7997c1fd
...
...
@@ -101,7 +101,15 @@ export class NextScript extends Component {
return
<
div
>
{
staticMarkup
?
null
:
<
script
dangerouslySetInnerHTML
=
{{
__html
:
`__NEXT_DATA__ =
${
htmlescape
(
__NEXT_DATA__
)}
; module={};`
__html
:
`
__NEXT_DATA__ =
${
htmlescape
(
__NEXT_DATA__
)}
module={}
__NEXT_LOADED_PAGES__ = []
__NEXT_REGISTER_PAGE = function (route, fn) {
__NEXT_LOADED_PAGES__.push({ route: route, fn: fn })
}
`
}}
/>
}
<
script
async
type
=
'
text/javascript
'
src
=
{
`/_next/
${
buildId
}
/page
${
pathname
}
`
}
/
>
<
script
async
type
=
'
text/javascript
'
src
=
{
`/_next/
${
buildId
}
/page/_error`
}
/
>
...
...
server/render.js
浏览文件 @
7997c1fd
...
...
@@ -122,20 +122,12 @@ export async function renderScriptError (req, res, page, error, customFields, op
if
(
error
.
code
===
'
ENOENT
'
)
{
res
.
setHeader
(
'
Content-Type
'
,
'
text/javascript
'
)
res
.
end
(
`
function loadPage
() {
window.__NEXT_REGISTER_PAGE('
${
page
}
', function
() {
var error = new Error('Page not exists:
${
page
}
')
error.statusCode = 404
__NEXT_PAGE_LOADER__.registerPage('
${
page
}
', function(cb) {
cb(error)
})
}
if (window.__NEXT_PAGE_LOADER__) {
loadPage()
} else {
window.__NEXT_LOADED_PAGES__ = window.__NEXT_LOADED_PAGES__ || []
window.__NEXT_LOADED_PAGES__.push(loadPage)
}
return { error: error }
})
`
)
return
}
...
...
@@ -147,19 +139,10 @@ export async function renderScriptError (req, res, page, error, customFields, op
}
res
.
end
(
`
function loadPage
() {
window.__NEXT_REGISTER_PAGE('
${
page
}
', function
() {
var error =
${
JSON
.
stringify
(
errorJson
)}
__NEXT_PAGE_LOADER__.registerPage('
${
page
}
', function(cb) {
cb(error)
})
}
if (window.__NEXT_PAGE_LOADER__) {
loadPage()
} else {
window.__NEXT_LOADED_PAGES__ = window.__NEXT_LOADED_PAGES__ || []
window.__NEXT_LOADED_PAGES__.push(loadPage)
}
return { error: error }
})
`
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录