Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
a0945c78
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,发现更多精彩内容 >>
提交
a0945c78
编写于
4月 03, 2017
作者:
A
Arunoda Susiripala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce script tag based page loading system.
上级
1e88331b
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
129 addition
and
3 deletion
+129
-3
client/index.js
client/index.js
+5
-1
lib/page-loader.js
lib/page-loader.js
+64
-0
server/build/plugins/json-pages-plugin.js
server/build/plugins/json-pages-plugin.js
+1
-1
server/build/plugins/pages-plugin.js
server/build/plugins/pages-plugin.js
+34
-0
server/build/webpack.js
server/build/webpack.js
+2
-0
server/index.js
server/index.js
+23
-1
未找到文件。
client/index.js
浏览文件 @
a0945c78
...
...
@@ -7,6 +7,7 @@ import App from '../lib/app'
import
evalScript
from
'
../lib/eval-script
'
import
{
loadGetInitialProps
,
getURL
}
from
'
../lib/utils
'
import
ErrorDebugComponent
from
'
../lib/error-debug
'
import
PageLoader
from
'
../lib/page-loader
'
// Polyfill Promise globally
// This is needed because Webpack2's dynamic loading(common chunks) code
...
...
@@ -24,11 +25,14 @@ const {
props
,
err
,
pathname
,
query
query
,
buildId
},
location
}
=
window
window
.
NEXT_PAGE_LOADER
=
new
PageLoader
(
buildId
)
const
Component
=
evalScript
(
component
).
default
const
ErrorComponent
=
evalScript
(
errorComponent
).
default
let
lastAppProps
...
...
lib/page-loader.js
0 → 100644
浏览文件 @
a0945c78
/* global window, document */
import
mitt
from
'
mitt
'
export
default
class
PageLoader
{
constructor
(
buildId
)
{
this
.
buildId
=
buildId
this
.
pageCache
=
{}
this
.
pageLoadedHandlers
=
{}
this
.
registerEvents
=
mitt
()
this
.
loadingRoutes
=
{}
}
loadPage
(
route
)
{
if
(
route
[
0
]
!==
'
/
'
)
{
throw
new
Error
(
'
Route name should start with a "/"
'
)
}
route
=
route
.
replace
(
/index$/
,
''
)
if
(
this
.
pageCache
[
route
])
{
return
Promise
.
resolve
(
this
.
pageCache
[
route
])
}
return
new
Promise
((
resolve
,
reject
)
=>
{
const
fire
=
({
error
,
page
})
=>
{
this
.
registerEvents
.
off
(
route
,
fire
)
if
(
error
)
{
reject
(
error
)
}
else
{
resolve
(
page
)
}
}
this
.
registerEvents
.
on
(
route
,
fire
)
// Load the script if not asked to load yet.
if
(
!
this
.
loadingRoutes
[
route
])
{
this
.
loadScript
(
route
)
this
.
loadingRoutes
[
route
]
=
true
}
})
}
loadScript
(
route
)
{
const
script
=
document
.
createElement
(
'
script
'
)
const
url
=
`/_next/
${
encodeURIComponent
(
this
.
buildId
)}
/page
${
route
}
`
script
.
src
=
url
script
.
type
=
'
text/javascript
'
script
.
onerror
=
()
=>
{
const
error
=
new
Error
(
`Error when loading route:
${
route
}
`
)
this
.
registerEvents
.
emit
(
route
,
{
error
})
}
document
.
body
.
appendChild
(
script
)
}
// This method if called by the route code.
registerPage
(
route
,
error
,
page
)
{
// add the page to the cache
this
.
pageCache
[
route
]
=
page
this
.
registerEvents
.
emit
(
route
,
{
error
,
page
})
}
}
server/build/plugins/json-pages-plugin.js
浏览文件 @
a0945c78
...
...
@@ -7,7 +7,7 @@ export default class JsonPagesPlugin {
pages
.
forEach
((
pageName
)
=>
{
const
page
=
compilation
.
assets
[
pageName
]
delete
compilation
.
assets
[
pageName
]
//
delete compilation.assets[pageName]
const
content
=
page
.
source
()
const
newContent
=
JSON
.
stringify
({
component
:
content
})
...
...
server/build/plugins/pages-plugin.js
0 → 100644
浏览文件 @
a0945c78
export
default
class
PagesPlugin
{
apply
(
compiler
)
{
const
isBundledPage
=
/^bundles
[/\\]
pages.*
\.
js$/
const
matchRouteName
=
/^bundles
[/\\]
pages
[/\\](
.*
)\.
js$/
compiler
.
plugin
(
'
after-compile
'
,
(
compilation
,
callback
)
=>
{
const
pages
=
Object
.
keys
(
compilation
.
namedChunks
)
.
map
(
key
=>
compilation
.
namedChunks
[
key
])
.
filter
(
chunk
=>
isBundledPage
.
test
(
chunk
.
name
))
pages
.
forEach
((
chunk
)
=>
{
const
page
=
compilation
.
assets
[
chunk
.
name
]
const
pageName
=
matchRouteName
.
exec
(
chunk
.
name
)[
1
]
const
routeName
=
`/
${
pageName
.
replace
(
/index$/
,
''
)}
`
const
content
=
page
.
source
()
const
newContent
=
`
var comp =
${
content
}
NEXT_PAGE_LOADER.registerPage('
${
routeName
}
', null, comp.default)
`
// Replace the current asset
// TODO: We need to move "client-bundles" back to "bundles" once we remove
// all the JSON eval stuff
delete
compilation
.
assets
[
chunk
.
name
]
compilation
.
assets
[
`client-bundles/pages/
${
pageName
}
.js`
]
=
{
source
:
()
=>
newContent
,
size
:
()
=>
newContent
.
length
}
})
callback
()
})
}
}
server/build/webpack.js
浏览文件 @
a0945c78
...
...
@@ -7,6 +7,7 @@ import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
import
CaseSensitivePathPlugin
from
'
case-sensitive-paths-webpack-plugin
'
import
UnlinkFilePlugin
from
'
./plugins/unlink-file-plugin
'
import
JsonPagesPlugin
from
'
./plugins/json-pages-plugin
'
import
PagesPlugin
from
'
./plugins/pages-plugin
'
import
CombineAssetsPlugin
from
'
./plugins/combine-assets-plugin
'
import
getConfig
from
'
../config
'
import
*
as
babelCore
from
'
babel-core
'
...
...
@@ -117,6 +118,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
'
process.env.NODE_ENV
'
:
JSON
.
stringify
(
dev
?
'
development
'
:
'
production
'
)
}),
new
JsonPagesPlugin
(),
new
PagesPlugin
(),
new
CaseSensitivePathPlugin
()
]
...
...
server/index.js
浏览文件 @
a0945c78
...
...
@@ -13,7 +13,7 @@ import {
}
from
'
./render
'
import
Router
from
'
./router
'
import
HotReloader
from
'
./hot-reloader
'
import
{
resolveFromList
}
from
'
./resolve
'
import
resolvePath
,
{
resolveFromList
}
from
'
./resolve
'
import
getConfig
from
'
./config
'
// We need to go up one more level since we are in the `dist` directory
import
pkg
from
'
../../package
'
...
...
@@ -127,6 +127,28 @@ export default class Server {
await
this
.
renderJSON
(
req
,
res
,
pathname
)
},
'
/_next/:buildId/page/:path*
'
:
async
(
req
,
res
,
params
)
=>
{
const
paths
=
params
.
path
||
[
''
]
const
pathname
=
`/
${
paths
.
join
(
'
/
'
)}
`
await
this
.
hotReloader
.
ensurePage
(
pathname
)
if
(
!
this
.
handleBuildId
(
params
.
buildId
,
res
))
{
res
.
setHeader
(
'
Content-Type
'
,
'
text/javascript
'
)
// TODO: Handle buildId mismatches properly.
res
.
end
(
`
var error = new Error('INVALID_BUILD_ID')
error.buildIdMismatched = true
NEXT_PAGE_LOADER.registerPage('
${
pathname
}
', error)
`
)
return
}
const
path
=
join
(
this
.
dir
,
'
.next
'
,
'
client-bundles
'
,
'
pages
'
,
pathname
)
const
realPath
=
await
resolvePath
(
path
)
await
this
.
serveStatic
(
req
,
res
,
realPath
)
},
'
/_next/:path+
'
:
async
(
req
,
res
,
params
)
=>
{
const
p
=
join
(
__dirname
,
'
..
'
,
'
client
'
,
...(
params
.
path
||
[]))
await
this
.
serveStatic
(
req
,
res
,
p
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录