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
737c283b
Commit
737c283b
authored
May 15, 2017
by
Arunoda Susiripala
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dynamic-import' into v3-beta
parents
d4c62b1b
f8317f15
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
38 changed files
with
888 additions
and
55 deletions
+888
-55
index.js
client/index.js
+12
-0
dynamic.js
dynamic.js
+1
-0
README.md
examples/with-dynamic-import/README.md
+27
-0
Counter.js
examples/with-dynamic-import/components/Counter.js
+19
-0
Header.js
examples/with-dynamic-import/components/Header.js
+19
-0
hello1.js
examples/with-dynamic-import/components/hello1.js
+3
-0
hello2.js
examples/with-dynamic-import/components/hello2.js
+3
-0
hello3.js
examples/with-dynamic-import/components/hello3.js
+3
-0
hello4.js
examples/with-dynamic-import/components/hello4.js
+3
-0
hello5.js
examples/with-dynamic-import/components/hello5.js
+3
-0
package.json
examples/with-dynamic-import/package.json
+19
-0
about.js
examples/with-dynamic-import/pages/about.js
+12
-0
index.js
examples/with-dynamic-import/pages/index.js
+43
-0
dynamic.js
lib/dynamic.js
+154
-0
page-loader.js
lib/page-loader.js
+30
-5
package.json
package.json
+2
-0
readme.md
readme.md
+91
-0
handle-import.js
server/build/babel/plugins/handle-import.js
+57
-0
preset.js
server/build/babel/preset.js
+2
-0
dynamic-chunks-plugin.js
server/build/plugins/dynamic-chunks-plugin.js
+39
-0
webpack.js
server/build/webpack.js
+6
-1
document.js
server/document.js
+42
-4
export.js
server/export.js
+11
-0
index.js
server/index.js
+13
-0
render.js
server/render.js
+3
-1
hello1.js
test/integration/basic/components/hello1.js
+3
-0
no-ssr-custom-loading.js
.../integration/basic/pages/dynamic/no-ssr-custom-loading.js
+8
-0
no-ssr.js
test/integration/basic/pages/dynamic/no-ssr.js
+5
-0
ssr.js
test/integration/basic/pages/dynamic/ssr.js
+5
-0
rendering.js
test/integration/basic/test/rendering.js
+15
-0
hello.js
test/integration/static/components/hello.js
+5
-0
next.config.js
test/integration/static/next.config.js
+1
-0
dynamic-imports.js
test/integration/static/pages/dynamic-imports.js
+15
-0
index.js
test/integration/static/pages/index.js
+3
-0
browser.js
test/integration/static/test/browser.js
+11
-0
ssr.js
test/integration/static/test/ssr.js
+5
-0
same-loop-promise.test.js
test/unit/same-loop-promise.test.js
+137
-0
yarn.lock
yarn.lock
+58
-44
No files found.
client/index.js
View file @
737c283b
...
@@ -24,6 +24,7 @@ const {
...
@@ -24,6 +24,7 @@ const {
pathname
,
pathname
,
query
,
query
,
buildId
,
buildId
,
chunks
,
assetPrefix
assetPrefix
},
},
location
location
...
@@ -37,7 +38,13 @@ window.__NEXT_LOADED_PAGES__.forEach(({ route, fn }) => {
...
@@ -37,7 +38,13 @@ window.__NEXT_LOADED_PAGES__.forEach(({ route, fn }) => {
})
})
delete
window
.
__NEXT_LOADED_PAGES__
delete
window
.
__NEXT_LOADED_PAGES__
window
.
__NEXT_LOADED_CHUNKS__
.
forEach
(({
chunkName
,
fn
})
=>
{
pageLoader
.
registerChunk
(
chunkName
,
fn
)
})
delete
window
.
__NEXT_LOADED_CHUNKS__
window
.
__NEXT_REGISTER_PAGE
=
pageLoader
.
registerPage
.
bind
(
pageLoader
)
window
.
__NEXT_REGISTER_PAGE
=
pageLoader
.
registerPage
.
bind
(
pageLoader
)
window
.
__NEXT_REGISTER_CHUNK
=
pageLoader
.
registerChunk
.
bind
(
pageLoader
)
const
headManager
=
new
HeadManager
()
const
headManager
=
new
HeadManager
()
const
appContainer
=
document
.
getElementById
(
'__next'
)
const
appContainer
=
document
.
getElementById
(
'__next'
)
...
@@ -49,6 +56,11 @@ export let ErrorComponent
...
@@ -49,6 +56,11 @@ export let ErrorComponent
let
Component
let
Component
export
default
async
()
=>
{
export
default
async
()
=>
{
// Wait for all the dynamic chunks to get loaded
for
(
const
chunkName
of
chunks
)
{
await
pageLoader
.
waitForChunk
(
chunkName
)
}
ErrorComponent
=
await
pageLoader
.
loadPage
(
'/_error'
)
ErrorComponent
=
await
pageLoader
.
loadPage
(
'/_error'
)
try
{
try
{
...
...
dynamic.js
0 → 100644
View file @
737c283b
module
.
exports
=
require
(
'./dist/lib/dynamic'
)
examples/with-dynamic-import/README.md
0 → 100644
View file @
737c283b
# Example app with dynamic-imports
## How to use
Download the example
[
or clone the repo
](
https://github.com/zeit/next.js
)
:
```
bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master |
tar
-xz
--strip
=
2 next.js-master/examples/shared-modules
cd
shared-modules
```
Install it and run:
```
bash
npm
install
npm run dev
```
Deploy it to the cloud with
[
now
](
https://zeit.co/now
)
(
[download](https://zeit.co/download
)
)
```
bash
now
```
## The idea behind the example
This examples shows how to dynamically import modules via
[
`import()`
](
https://github.com/tc39/proposal-dynamic-import
)
API
examples/with-dynamic-import/components/Counter.js
0 → 100644
View file @
737c283b
import
React
from
'react'
let
count
=
0
export
default
class
Counter
extends
React
.
Component
{
add
()
{
count
+=
1
this
.
forceUpdate
()
}
render
()
{
return
(
<
div
>
<
p
>
Count
is
:
{
count
}
<
/p
>
<
button
onClick
=
{()
=>
this
.
add
()}
>
Add
<
/button
>
<
/div
>
)
}
}
examples/with-dynamic-import/components/Header.js
0 → 100644
View file @
737c283b
import
Link
from
'next/link'
export
default
()
=>
(
<
div
>
<
Link
href
=
'/'
>
<
a
style
=
{
styles
.
a
}
>
Home
<
/a
>
<
/Link
>
<
Link
href
=
'/about'
>
<
a
style
=
{
styles
.
a
}
>
About
<
/a
>
<
/Link
>
<
/div
>
)
const
styles
=
{
a
:
{
marginRight
:
10
}
}
examples/with-dynamic-import/components/hello1.js
0 → 100644
View file @
737c283b
export
default
()
=>
(
<
p
>
Hello
World
1
(
imported
dynamiclly
)
<
/p
>
)
examples/with-dynamic-import/components/hello2.js
0 → 100644
View file @
737c283b
export
default
()
=>
(
<
p
>
Hello
World
2
(
imported
dynamiclly
)
<
/p
>
)
examples/with-dynamic-import/components/hello3.js
0 → 100644
View file @
737c283b
export
default
()
=>
(
<
p
>
Hello
World
3
(
imported
dynamiclly
)
<
/p
>
)
examples/with-dynamic-import/components/hello4.js
0 → 100644
View file @
737c283b
export
default
()
=>
(
<
p
>
Hello
World
4
(
imported
dynamiclly
)
<
/p
>
)
examples/with-dynamic-import/components/hello5.js
0 → 100644
View file @
737c283b
export
default
()
=>
(
<
p
>
Hello
World
5
(
imported
dynamiclly
)
<
/p
>
)
examples/with-dynamic-import/package.json
0 → 100644
View file @
737c283b
{
"name"
:
"with-dynamic-import"
,
"version"
:
"1.0.0"
,
"description"
:
"This example features:"
,
"main"
:
"index.js"
,
"scripts"
:
{
"dev"
:
"next"
,
"build"
:
"next build"
,
"start"
:
"next start"
},
"dependencies"
:
{
"async-reactor"
:
"^1.1.1"
,
"next"
:
"*"
,
"react"
:
"^15.4.2"
,
"react-dom"
:
"^15.4.2"
},
"author"
:
""
,
"license"
:
"ISC"
}
examples/with-dynamic-import/pages/about.js
0 → 100644
View file @
737c283b
import
Header
from
'../components/Header'
import
Counter
from
'../components/Counter'
const
About
=
()
=>
(
<
div
>
<
Header
/>
<
p
>
This
is
the
about
page
.
<
/p
>
<
Counter
/>
<
/div
>
)
export
default
About
examples/with-dynamic-import/pages/index.js
0 → 100644
View file @
737c283b
import
React
from
'react'
import
Header
from
'../components/Header'
import
Counter
from
'../components/Counter'
import
dynamic
from
'next/dynamic'
import
{
asyncReactor
}
from
'async-reactor'
const
DynamicComponent
=
dynamic
(
import
(
'../components/hello1'
))
const
DynamicComponentWithCustomLoading
=
dynamic
(
import
(
'../components/hello2'
),
{
loading
:
()
=>
(
<
p
>
...
<
/p>
)
}
)
const
DynamicComponentWithNoSSR
=
dynamic
(
import
(
'../components/hello3'
),
{
ssr
:
false
}
)
const
DynamicComponentWithAsyncReactor
=
asyncReactor
(
async
()
=>
{
const
Hello4
=
await
import
(
'../components/hello4'
)
return
(
<
Hello4
/>
)
})
const
DynamicComponent5
=
dynamic
(
import
(
'../components/hello5'
))
export
default
()
=>
(
<
div
>
<
Header
/>
<
DynamicComponent
/>
<
DynamicComponentWithCustomLoading
/>
<
DynamicComponentWithNoSSR
/>
<
DynamicComponentWithAsyncReactor
/>
{
/*
Since DynamicComponent5 does not render in the client,
it won't get downloaded.
*/
}
{
React
.
noSuchField
===
true
?
<
DynamicComponent5
/>
:
null
}
<
p
>
HOME
PAGE
is
here
!<
/p
>
<
Counter
/>
<
/div
>
)
lib/dynamic.js
0 → 100644
View file @
737c283b
import
React
from
'react'
let
currentChunks
=
[]
export
default
function
dynamicComponent
(
promise
,
options
=
{})
{
return
class
DynamicComponent
extends
React
.
Component
{
constructor
(...
args
)
{
super
(...
args
)
this
.
LoadingComponent
=
options
.
loading
?
options
.
loading
:
()
=>
(
<
p
>
loading
...
<
/p>
)
this
.
ssr
=
options
.
ssr
===
false
?
options
.
ssr
:
true
this
.
state
=
{
AsyncComponent
:
null
}
this
.
isServer
=
typeof
window
===
'undefined'
if
(
this
.
ssr
)
{
this
.
loadComponent
()
}
}
loadComponent
()
{
promise
.
then
((
AsyncComponent
)
=>
{
// Set a readable displayName for the wrapper component
const
asyncCompName
=
AsyncComponent
.
displayName
||
AsyncComponent
.
name
if
(
asyncCompName
)
{
DynamicComponent
.
displayName
=
`DynamicComponent for
${
asyncCompName
}
`
}
if
(
this
.
mounted
)
{
this
.
setState
({
AsyncComponent
})
}
else
{
if
(
this
.
isServer
)
{
currentChunks
.
push
(
AsyncComponent
.
__webpackChunkName
)
}
this
.
state
.
AsyncComponent
=
AsyncComponent
}
})
}
componentDidMount
()
{
this
.
mounted
=
true
if
(
!
this
.
ssr
)
{
this
.
loadComponent
()
}
}
render
()
{
const
{
AsyncComponent
}
=
this
.
state
const
{
LoadingComponent
}
=
this
if
(
!
AsyncComponent
)
return
(
<
LoadingComponent
{...
this
.
props
}
/>
)
return
<
AsyncComponent
{...
this
.
props
}
/
>
}
}
}
export
function
flushChunks
()
{
const
chunks
=
currentChunks
currentChunks
=
[]
return
chunks
}
export
class
SameLoopPromise
{
constructor
(
cb
)
{
this
.
onResultCallbacks
=
[]
this
.
onErrorCallbacks
=
[]
this
.
cb
=
cb
}
setResult
(
result
)
{
this
.
gotResult
=
true
this
.
result
=
result
this
.
onResultCallbacks
.
forEach
((
cb
)
=>
cb
(
result
))
this
.
onResultCallbacks
=
[]
}
setError
(
error
)
{
this
.
gotError
=
true
this
.
error
=
error
this
.
onErrorCallbacks
.
forEach
((
cb
)
=>
cb
(
error
))
this
.
onErrorCallbacks
=
[]
}
then
(
onResult
,
onError
)
{
this
.
runIfNeeded
()
const
promise
=
new
SameLoopPromise
()
const
handleError
=
()
=>
{
if
(
onError
)
{
promise
.
setResult
(
onError
(
this
.
error
))
}
else
{
promise
.
setError
(
this
.
error
)
}
}
const
handleResult
=
()
=>
{
promise
.
setResult
(
onResult
(
this
.
result
))
}
if
(
this
.
gotResult
)
{
handleResult
()
return
promise
}
if
(
this
.
gotError
)
{
handleError
()
return
promise
}
this
.
onResultCallbacks
.
push
(
handleResult
)
this
.
onErrorCallbacks
.
push
(
handleError
)
return
promise
}
catch
(
onError
)
{
this
.
runIfNeeded
()
const
promise
=
new
SameLoopPromise
()
const
handleError
=
()
=>
{
promise
.
setResult
(
onError
(
this
.
error
))
}
const
handleResult
=
()
=>
{
promise
.
setResult
(
this
.
result
)
}
if
(
this
.
gotResult
)
{
handleResult
()
return
promise
}
if
(
this
.
gotError
)
{
handleError
()
return
promise
}
this
.
onErrorCallbacks
.
push
(
handleError
)
this
.
onResultCallbacks
.
push
(
handleResult
)
return
promise
}
runIfNeeded
()
{
if
(
!
this
.
cb
)
return
if
(
this
.
ran
)
return
this
.
ran
=
true
this
.
cb
(
(
result
)
=>
this
.
setResult
(
result
),
(
error
)
=>
this
.
setError
(
error
)
)
}
}
lib/page-loader.js
View file @
737c283b
...
@@ -10,8 +10,11 @@ export default class PageLoader {
...
@@ -10,8 +10,11 @@ export default class PageLoader {
this
.
pageCache
=
{}
this
.
pageCache
=
{}
this
.
pageLoadedHandlers
=
{}
this
.
pageLoadedHandlers
=
{}
this
.
r
egisterEvents
=
mitt
()
this
.
pageR
egisterEvents
=
mitt
()
this
.
loadingRoutes
=
{}
this
.
loadingRoutes
=
{}
this
.
chunkRegisterEvents
=
mitt
()
this
.
loadedChunks
=
{}
}
}
normalizeRoute
(
route
)
{
normalizeRoute
(
route
)
{
...
@@ -37,7 +40,7 @@ export default class PageLoader {
...
@@ -37,7 +40,7 @@ export default class PageLoader {
return
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
fire
=
({
error
,
page
})
=>
{
const
fire
=
({
error
,
page
})
=>
{
this
.
r
egisterEvents
.
off
(
route
,
fire
)
this
.
pageR
egisterEvents
.
off
(
route
,
fire
)
if
(
error
)
{
if
(
error
)
{
reject
(
error
)
reject
(
error
)
...
@@ -46,7 +49,7 @@ export default class PageLoader {
...
@@ -46,7 +49,7 @@ export default class PageLoader {
}
}
}
}
this
.
r
egisterEvents
.
on
(
route
,
fire
)
this
.
pageR
egisterEvents
.
on
(
route
,
fire
)
// If the page is loading via SSR, we need to wait for it
// If the page is loading via SSR, we need to wait for it
// rather downloading it again.
// rather downloading it again.
...
@@ -75,7 +78,7 @@ export default class PageLoader {
...
@@ -75,7 +78,7 @@ export default class PageLoader {
script
.
type
=
'text/javascript'
script
.
type
=
'text/javascript'
script
.
onerror
=
()
=>
{
script
.
onerror
=
()
=>
{
const
error
=
new
Error
(
`Error when loading route:
${
route
}
`
)
const
error
=
new
Error
(
`Error when loading route:
${
route
}
`
)
this
.
r
egisterEvents
.
emit
(
route
,
{
error
})
this
.
pageR
egisterEvents
.
emit
(
route
,
{
error
})
}
}
document
.
body
.
appendChild
(
script
)
document
.
body
.
appendChild
(
script
)
...
@@ -86,7 +89,7 @@ export default class PageLoader {
...
@@ -86,7 +89,7 @@ export default class PageLoader {
const
register
=
()
=>
{
const
register
=
()
=>
{
const
{
error
,
page
}
=
regFn
()
const
{
error
,
page
}
=
regFn
()
this
.
pageCache
[
route
]
=
{
error
,
page
}
this
.
pageCache
[
route
]
=
{
error
,
page
}
this
.
r
egisterEvents
.
emit
(
route
,
{
error
,
page
})
this
.
pageR
egisterEvents
.
emit
(
route
,
{
error
,
page
})
}
}
// Wait for webpack to became idle if it's not.
// Wait for webpack to became idle if it's not.
...
@@ -106,6 +109,28 @@ export default class PageLoader {
...
@@ -106,6 +109,28 @@ export default class PageLoader {
}
}
}
}
registerChunk
(
chunkName
,
regFn
)
{
const
chunk
=
regFn
()
this
.
loadedChunks
[
chunkName
]
=
true
this
.
chunkRegisterEvents
.
emit
(
chunkName
,
chunk
)
}
waitForChunk
(
chunkName
,
regFn
)
{
const
loadedChunk
=
this
.
loadedChunks
[
chunkName
]
if
(
loadedChunk
)
{
return
Promise
.
resolve
(
true
)
}
return
new
Promise
((
resolve
)
=>
{
const
register
=
(
chunk
)
=>
{
this
.
chunkRegisterEvents
.
off
(
chunkName
,
register
)
resolve
(
chunk
)
}
this
.
chunkRegisterEvents
.
on
(
chunkName
,
register
)
})
}
clearCache
(
route
)
{
clearCache
(
route
)
{
route
=
this
.
normalizeRoute
(
route
)
route
=
this
.
normalizeRoute
(
route
)
delete
this
.
pageCache
[
route
]
delete
this
.
pageCache
[
route
]
...
...
package.json
View file @
737c283b
...
@@ -49,6 +49,7 @@
...
@@ -49,6 +49,7 @@
"
babel-loader
"
:
"7.0.0"
,
"
babel-loader
"
:
"7.0.0"
,
"
babel-plugin-module-resolver
"
:
"2.6.2"
,
"
babel-plugin-module-resolver
"
:
"2.6.2"
,
"
babel-plugin-react-require
"
:
"3.0.0"
,
"
babel-plugin-react-require
"
:
"3.0.0"
,
"
babel-plugin-syntax-dynamic-import
"
:
"6.18.0"
,
"
babel-plugin-transform-class-properties
"
:
"6.24.1"
,
"
babel-plugin-transform-class-properties
"
:
"6.24.1"
,
"
babel-plugin-transform-es2015-modules-commonjs
"
:
"6.24.1"
,
"
babel-plugin-transform-es2015-modules-commonjs
"
:
"6.24.1"
,
"
babel-plugin-transform-object-rest-spread
"
:
"6.22.0"
,
"
babel-plugin-transform-object-rest-spread
"
:
"6.22.0"
,
...
@@ -58,6 +59,7 @@
...
@@ -58,6 +59,7 @@
"
babel-preset-env
"
:
"1.3.3"
,
"
babel-preset-env
"
:
"1.3.3"
,
"
babel-preset-react
"
:
"6.24.1"
,
"
babel-preset-react
"
:
"6.24.1"
,
"
babel-runtime
"
:
"6.23.0"
,
"
babel-runtime
"
:
"6.23.0"
,
"
babel-template
"
:
"6.24.1"
,
"
case-sensitive-paths-webpack-plugin
"
:
"2.0.0"
,
"
case-sensitive-paths-webpack-plugin
"
:
"2.0.0"
,
"
cross-spawn
"
:
"5.1.0"
,
"
cross-spawn
"
:
"5.1.0"
,
"
del
"
:
"2.2.2"
,
"
del
"
:
"2.2.2"
,
...
...
readme.md
View file @
737c283b
...
@@ -32,6 +32,7 @@ Next.js is a minimalistic framework for server-rendered React applications.
...
@@ -32,6 +32,7 @@ Next.js is a minimalistic framework for server-rendered React applications.
-
[
With `<Link>`
](
#with-link-1
)
-
[
With `<Link>`
](
#with-link-1
)
-
[
Imperatively
](
#imperatively-1
)
-
[
Imperatively
](
#imperatively-1
)
-
[
Custom server and routing
](
#custom-server-and-routing
)
-
[
Custom server and routing
](
#custom-server-and-routing
)
-
[
Dynamic Imports
](
#dynamic-imports
)
-
[
Custom `<Document>`
](
#custom-document
)
-
[
Custom `<Document>`
](
#custom-document
)
-
[
Custom error handling
](
#custom-error-handling
)
-
[
Custom error handling
](
#custom-error-handling
)
-
[
Custom configuration
](
#custom-configuration
)
-
[
Custom configuration
](
#custom-configuration
)
...
@@ -579,6 +580,96 @@ Supported options:
...
@@ -579,6 +580,96 @@ Supported options:
Then, change your
`start`
script to
`NODE_ENV=production node server.js`
.
Then, change your
`start`
script to
`NODE_ENV=production node server.js`
.
### Dynamic Import
<p><details>
<summary><b>
Examples
</b></summary>
<ul>
<li><a
href=
"./examples/with-dynamic-import"
>
With Dynamic Import
</a></li>
</ul>
</details></p>
Next.js supports TC39
[
dynamic import proposal
](
https://github.com/tc39/proposal-dynamic-import
)
for JavaScript.
With that, you could import JavaScript modules (inc. React Components) dynamically and work with them.
You can think dynamic imports as another way to split your code into manageable chunks.
Since Next.js supports dynamic imports with SSR, you could do amazing things with it.
Here are a few ways to use dynamic imports.
#### 1. Basic Usage (Also does SSR)
```
js
import
dynamic
from
'next/dynamic'
const
DynamicComponent
=
dynamic
(
import
(
'../components/hello'
))
export
default
()
=>
(
<
div
>
<
Header
/>
<
DynamicComponent
/>
<
p
>
HOME
PAGE
is
here
!<
/p
>
<
/div
>
)
```
#### 2. With Custom Loading Component
```
js
import
dynamic
from
'next/dynamic'
const
DynamicComponentWithCustomLoading
=
dynamic
(
import
(
'../components/hello2'
),
{
loading
:
()
=>
(
<
p
>
...
<
/p>
)
}
)
export
default
()
=>
(
<
div
>
<
Header
/>
<
DynamicComponentWithCustomLoading
/>
<
p
>
HOME
PAGE
is
here
!<
/p
>
<
/div
>
)
```
#### 3. With No SSR
```
js
import
dynamic
from
'next/dynamic'
const
DynamicComponentWithNoSSR
=
dynamic
(
import
(
'../components/hello3'
),
{
ssr
:
false
}
)
export
default
()
=>
(
<
div
>
<
Header
/>
<
DynamicComponentWithNoSSR
/>
<
p
>
HOME
PAGE
is
here
!<
/p
>
<
/div
>
)
```
#### 4. With [async-reactor](https://github.com/xtuc/async-reactor)
> SSR support is not available here
```
js
import
{
asyncReactor
}
from
'async-reactor'
const
DynamicComponentWithAsyncReactor
=
asyncReactor
(
async
()
=>
{
const
Hello4
=
await
import
(
'../components/hello4'
)
return
(
<
Hello4
/>
)
})
export
default
()
=>
(
<
div
>
<
Header
/>
<
DynamicComponentWithAsyncReactor
/>
<
p
>
HOME
PAGE
is
here
!<
/p
>
<
/div
>
)
```
### Custom `<Document>`
### Custom `<Document>`
<p><details>
<p><details>
...
...
server/build/babel/plugins/handle-import.js
0 → 100644
View file @
737c283b
// Based on https://github.com/airbnb/babel-plugin-dynamic-import-webpack
// We've added support for SSR with this version
import
template
from
'babel-template'
import
syntax
from
'babel-plugin-syntax-dynamic-import'
import
UUID
from
'uuid'
const
TYPE_IMPORT
=
'Import'
const
buildImport
=
(
args
)
=>
(
template
(
`
(
typeof window === 'undefined' ?
new (require('next/dynamic').SameLoopPromise)((resolve, reject) => {
eval('require.ensure = function (deps, callback) { callback(require) }')
require.ensure([], (require) => {
let m = require(SOURCE)
m = m.default || m
m.__webpackChunkName = '
${
args
.
name
}
.js'
resolve(m);
}, 'chunks/
${
args
.
name
}
.js');
})
:
new (require('next/dynamic').SameLoopPromise)((resolve, reject) => {
const weakId = require.resolveWeak(SOURCE)
try {
const weakModule = __webpack_require__(weakId)
return resolve(weakModule.default || weakModule)
} catch (err) {}
require.ensure([], (require) => {
try {
let m = require(SOURCE)
m = m.default || m
resolve(m)
} catch(error) {
reject(error)
}
}, 'chunks/
${
args
.
name
}
.js');
})
)
`
))
export
default
()
=>
({
inherits
:
syntax
,
visitor
:
{
CallExpression
(
path
)
{
if
(
path
.
node
.
callee
.
type
===
TYPE_IMPORT
)
{
const
newImport
=
buildImport
({
name
:
UUID
.
v4
()
})({
SOURCE
:
path
.
node
.
arguments
})
path
.
replaceWith
(
newImport
)
}
}
}
})
server/build/babel/preset.js
View file @
737c283b
...
@@ -20,6 +20,7 @@ module.exports = {
...
@@ -20,6 +20,7 @@ module.exports = {
],
],
plugins
:
[
plugins
:
[
require
.
resolve
(
'babel-plugin-react-require'
),
require
.
resolve
(
'babel-plugin-react-require'
),
require
.
resolve
(
'./plugins/handle-import'
),
require
.
resolve
(
'babel-plugin-transform-object-rest-spread'
),
require
.
resolve
(
'babel-plugin-transform-object-rest-spread'
),
require
.
resolve
(
'babel-plugin-transform-class-properties'
),
require
.
resolve
(
'babel-plugin-transform-class-properties'
),
require
.
resolve
(
'babel-plugin-transform-runtime'
),
require
.
resolve
(
'babel-plugin-transform-runtime'
),
...
@@ -33,6 +34,7 @@ module.exports = {
...
@@ -33,6 +34,7 @@ module.exports = {
'next/link'
:
relativeResolve
(
'../../../lib/link'
),
'next/link'
:
relativeResolve
(
'../../../lib/link'
),
'next/prefetch'
:
relativeResolve
(
'../../../lib/prefetch'
),
'next/prefetch'
:
relativeResolve
(
'../../../lib/prefetch'
),
'next/css'
:
relativeResolve
(
'../../../lib/css'
),
'next/css'
:
relativeResolve
(
'../../../lib/css'
),
'next/dynamic'
:
relativeResolve
(
'../../../lib/dynamic'
),
'next/head'
:
relativeResolve
(
'../../../lib/head'
),
'next/head'
:
relativeResolve
(
'../../../lib/head'
),
'next/document'
:
relativeResolve
(
'../../../server/document'
),
'next/document'
:
relativeResolve
(
'../../../server/document'
),
'next/router'
:
relativeResolve
(
'../../../lib/router'
),
'next/router'
:
relativeResolve
(
'../../../lib/router'
),
...
...
server/build/plugins/dynamic-chunks-plugin.js
0 → 100644
View file @
737c283b
export
default
class
PagesPlugin
{
apply
(
compiler
)
{
const
isImportChunk
=
/^chunks
[/\\]
.*
\.
js$/
const
matchChunkName
=
/^chunks
[/\\](
.*
)
$/
compiler
.
plugin
(
'after-compile'
,
(
compilation
,
callback
)
=>
{
const
chunks
=
Object
.
keys
(
compilation
.
namedChunks
)
.
map
(
key
=>
compilation
.
namedChunks
[
key
])
.
filter
(
chunk
=>
isImportChunk
.
test
(
chunk
.
name
))
chunks
.
forEach
((
chunk
)
=>
{
const
asset
=
compilation
.
assets
[
chunk
.
name
]
if
(
!
asset
)
return
const
chunkName
=
matchChunkName
.
exec
(
chunk
.
name
)[
1
]
const
content
=
asset
.
source
()
const
newContent
=
`
window.__NEXT_REGISTER_CHUNK('
${
chunkName
}
', function() {
${
content
}
})
`
// Replace the exisiting chunk with the new content
compilation
.
assets
[
chunk
.
name
]
=
{
source
:
()
=>
newContent
,
size
:
()
=>
newContent
.
length
}
// This is to support, webpack dynamic import support with HMR
compilation
.
assets
[
`chunks/
${
chunk
.
id
}
`
]
=
{
source
:
()
=>
newContent
,
size
:
()
=>
newContent
.
length
}
})
callback
()
})
}
}
server/build/webpack.js
View file @
737c283b
...
@@ -7,6 +7,7 @@ import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
...
@@ -7,6 +7,7 @@ import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
import
CaseSensitivePathPlugin
from
'case-sensitive-paths-webpack-plugin'
import
CaseSensitivePathPlugin
from
'case-sensitive-paths-webpack-plugin'
import
UnlinkFilePlugin
from
'./plugins/unlink-file-plugin'
import
UnlinkFilePlugin
from
'./plugins/unlink-file-plugin'
import
PagesPlugin
from
'./plugins/pages-plugin'
import
PagesPlugin
from
'./plugins/pages-plugin'
import
DynamicChunksPlugin
from
'./plugins/dynamic-chunks-plugin'
import
CombineAssetsPlugin
from
'./plugins/combine-assets-plugin'
import
CombineAssetsPlugin
from
'./plugins/combine-assets-plugin'
import
getConfig
from
'../config'
import
getConfig
from
'../config'
import
*
as
babelCore
from
'babel-core'
import
*
as
babelCore
from
'babel-core'
...
@@ -116,6 +117,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
...
@@ -116,6 +117,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
'process.env.NODE_ENV'
:
JSON
.
stringify
(
dev
?
'development'
:
'production'
)
'process.env.NODE_ENV'
:
JSON
.
stringify
(
dev
?
'development'
:
'production'
)
}),
}),
new
PagesPlugin
(),
new
PagesPlugin
(),
new
DynamicChunksPlugin
(),
new
CaseSensitivePathPlugin
()
new
CaseSensitivePathPlugin
()
]
]
...
@@ -219,6 +221,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
...
@@ -219,6 +221,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
'next/link'
:
relativeResolve
(
'../../lib/link'
),
'next/link'
:
relativeResolve
(
'../../lib/link'
),
'next/prefetch'
:
relativeResolve
(
'../../lib/prefetch'
),
'next/prefetch'
:
relativeResolve
(
'../../lib/prefetch'
),
'next/css'
:
relativeResolve
(
'../../lib/css'
),
'next/css'
:
relativeResolve
(
'../../lib/css'
),
'next/dynamic'
:
relativeResolve
(
'../../lib/dynamic'
),
'next/head'
:
relativeResolve
(
'../../lib/head'
),
'next/head'
:
relativeResolve
(
'../../lib/head'
),
'next/document'
:
relativeResolve
(
'../../server/document'
),
'next/document'
:
relativeResolve
(
'../../server/document'
),
'next/router'
:
relativeResolve
(
'../../lib/router'
),
'next/router'
:
relativeResolve
(
'../../lib/router'
),
...
@@ -274,7 +277,9 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
...
@@ -274,7 +277,9 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
// append hash id for cache busting
// append hash id for cache busting
return
`webpack:///
${
resourcePath
}
?
${
id
}
`
return
`webpack:///
${
resourcePath
}
?
${
id
}
`
}
},
// This saves chunks with the name given via require.ensure()
chunkFilename
:
'[name]'
},
},
resolve
:
{
resolve
:
{
modules
:
[
modules
:
[
...
...
server/document.js
View file @
737c283b
...
@@ -5,9 +5,9 @@ import flush from 'styled-jsx/server'
...
@@ -5,9 +5,9 @@ import flush from 'styled-jsx/server'
export
default
class
Document
extends
Component
{
export
default
class
Document
extends
Component
{
static
getInitialProps
({
renderPage
})
{
static
getInitialProps
({
renderPage
})
{
const
{
html
,
head
,
errorHtml
}
=
renderPage
()
const
{
html
,
head
,
errorHtml
,
chunks
}
=
renderPage
()
const
styles
=
flush
()
const
styles
=
flush
()
return
{
html
,
head
,
errorHtml
,
styles
}
return
{
html
,
head
,
errorHtml
,
chunks
,
styles
}
}
}
static
childContextTypes
=
{
static
childContextTypes
=
{
...
@@ -65,6 +65,19 @@ export class Head extends Component {
...
@@ -65,6 +65,19 @@ export class Head extends Component {
]
]
}
}
getPreloadDynamicChunks
()
{
const
{
chunks
,
__NEXT_DATA__
}
=
this
.
context
.
_documentProps
let
{
assetPrefix
}
=
__NEXT_DATA__
return
chunks
.
map
((
chunk
)
=>
(
<
link
key
=
{
chunk
}
rel
=
'preload'
href
=
{
`
${
assetPrefix
}
/_next/webpack/chunks/
${
chunk
}
`
}
as
=
'script'
/>
))
}
render
()
{
render
()
{
const
{
head
,
styles
,
__NEXT_DATA__
}
=
this
.
context
.
_documentProps
const
{
head
,
styles
,
__NEXT_DATA__
}
=
this
.
context
.
_documentProps
const
{
pathname
,
buildId
,
assetPrefix
,
nextExport
}
=
__NEXT_DATA__
const
{
pathname
,
buildId
,
assetPrefix
,
nextExport
}
=
__NEXT_DATA__
...
@@ -73,6 +86,7 @@ export class Head extends Component {
...
@@ -73,6 +86,7 @@ export class Head extends Component {
return
<
head
>
return
<
head
>
<
link
rel
=
'preload'
href
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page
${
pagePathname
}
`
}
as
=
'script'
/>
<
link
rel
=
'preload'
href
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page
${
pagePathname
}
`
}
as
=
'script'
/>
<
link
rel
=
'preload'
href
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page/_error/index.js`
}
as
=
'script'
/>
<
link
rel
=
'preload'
href
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page/_error/index.js`
}
as
=
'script'
/>
{
this
.
getPreloadDynamicChunks
()}
{
this
.
getPreloadMainLinks
()}
{
this
.
getPreloadMainLinks
()}
{(
head
||
[]).
map
((
h
,
i
)
=>
React
.
cloneElement
(
h
,
{
key
:
i
}))}
{(
head
||
[]).
map
((
h
,
i
)
=>
React
.
cloneElement
(
h
,
{
key
:
i
}))}
{
styles
||
null
}
{
styles
||
null
}
...
@@ -132,26 +146,50 @@ export class NextScript extends Component {
...
@@ -132,26 +146,50 @@ export class NextScript extends Component {
return
[
this
.
getChunkScript
(
'app.js'
,
{
async
:
true
})]
return
[
this
.
getChunkScript
(
'app.js'
,
{
async
:
true
})]
}
}
getDynamicChunks
()
{
const
{
chunks
,
__NEXT_DATA__
}
=
this
.
context
.
_documentProps
let
{
assetPrefix
}
=
__NEXT_DATA__
return
(
<
div
>
{
chunks
.
map
((
chunk
)
=>
(
<
script
async
key
=
{
chunk
}
type
=
'text/javascript'
src
=
{
`
${
assetPrefix
}
/_next/webpack/chunks/
${
chunk
}
`
}
/
>
))}
<
/div
>
)
}
render
()
{
render
()
{
const
{
staticMarkup
,
__NEXT_DATA__
}
=
this
.
context
.
_documentProps
const
{
staticMarkup
,
__NEXT_DATA__
,
chunks
}
=
this
.
context
.
_documentProps
const
{
pathname
,
nextExport
,
buildId
,
assetPrefix
}
=
__NEXT_DATA__
const
{
pathname
,
nextExport
,
buildId
,
assetPrefix
}
=
__NEXT_DATA__
const
pagePathname
=
getPagePathname
(
pathname
,
nextExport
)
const
pagePathname
=
getPagePathname
(
pathname
,
nextExport
)
__NEXT_DATA__
.
chunks
=
chunks
return
<
div
>
return
<
div
>
{
staticMarkup
?
null
:
<
script
dangerouslySetInnerHTML
=
{{
{
staticMarkup
?
null
:
<
script
dangerouslySetInnerHTML
=
{{
__html
:
`
__html
:
`
__NEXT_DATA__ =
${
htmlescape
(
__NEXT_DATA__
)}
__NEXT_DATA__ =
${
htmlescape
(
__NEXT_DATA__
)}
module={}
module={}
__NEXT_LOADED_PAGES__ = []
__NEXT_LOADED_PAGES__ = []
__NEXT_LOADED_CHUNKS__ = []
__NEXT_REGISTER_PAGE = function (route, fn) {
__NEXT_REGISTER_PAGE = function (route, fn) {
__NEXT_LOADED_PAGES__.push({ route: route, fn: fn })
__NEXT_LOADED_PAGES__.push({ route: route, fn: fn })
}
}
__NEXT_REGISTER_CHUNK = function (chunkName, fn) {
__NEXT_LOADED_CHUNKS__.push({ chunkName: chunkName, fn: fn })
}
`
`
}}
/>
}
}}
/>
}
<
script
async
id
=
{
`__NEXT_PAGE__
${
pathname
}
`
}
type
=
'text/javascript'
src
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page
${
pagePathname
}
`
}
/
>
<
script
async
id
=
{
`__NEXT_PAGE__
${
pathname
}
`
}
type
=
'text/javascript'
src
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page
${
pagePathname
}
`
}
/
>
<
script
async
id
=
{
`__NEXT_PAGE__/_error`
}
type
=
'text/javascript'
src
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page/_error/index.js`
}
/
>
<
script
async
id
=
{
`__NEXT_PAGE__/_error`
}
type
=
'text/javascript'
src
=
{
`
${
assetPrefix
}
/_next/
${
buildId
}
/page/_error/index.js`
}
/
>
{
staticMarkup
?
null
:
this
.
getDynamicChunks
()}
{
staticMarkup
?
null
:
this
.
getScripts
()}
{
staticMarkup
?
null
:
this
.
getScripts
()}
<
/div
>
<
/div
>
}
}
...
...
server/export.js
View file @
737c283b
...
@@ -44,6 +44,17 @@ export default async function (dir, options) {
...
@@ -44,6 +44,17 @@ export default async function (dir, options) {
)
)
}
}
// Copy dynamic import chunks
if
(
existsSync
(
join
(
nextDir
,
'chunks'
)))
{
log
(
' copying dynamic import chunks'
)
await
mkdirp
(
join
(
outDir
,
'_next'
,
'webpack'
))
await
cp
(
join
(
nextDir
,
'chunks'
),
join
(
outDir
,
'_next'
,
'webpack'
,
'chunks'
)
)
}
await
copyPages
(
nextDir
,
outDir
,
buildId
)
await
copyPages
(
nextDir
,
outDir
,
buildId
)
// Get the exportPathMap from the `next.config.js`
// Get the exportPathMap from the `next.config.js`
...
...
server/index.js
View file @
737c283b
...
@@ -99,6 +99,19 @@ export default class Server {
...
@@ -99,6 +99,19 @@ export default class Server {
await
this
.
serveStatic
(
req
,
res
,
p
)
await
this
.
serveStatic
(
req
,
res
,
p
)
},
},
// This is to support, webpack dynamic imports in production.
'/_next/webpack/chunks/:name'
:
async
(
req
,
res
,
params
)
=>
{
res
.
setHeader
(
'Cache-Control'
,
'max-age=365000000, immutable'
)
const
p
=
join
(
this
.
dir
,
'.next'
,
'chunks'
,
params
.
name
)
await
this
.
serveStatic
(
req
,
res
,
p
)
},
// This is to support, webpack dynamic import support with HMR
'/_next/webpack/:id'
:
async
(
req
,
res
,
params
)
=>
{
const
p
=
join
(
this
.
dir
,
'.next'
,
'chunks'
,
params
.
id
)
await
this
.
serveStatic
(
req
,
res
,
p
)
},
'/_next/:hash/manifest.js'
:
async
(
req
,
res
,
params
)
=>
{
'/_next/:hash/manifest.js'
:
async
(
req
,
res
,
params
)
=>
{
this
.
handleBuildHash
(
'manifest.js'
,
params
.
hash
,
res
)
this
.
handleBuildHash
(
'manifest.js'
,
params
.
hash
,
res
)
const
p
=
join
(
this
.
dir
,
`
${
this
.
dist
}
/manifest.js`
)
const
p
=
join
(
this
.
dir
,
`
${
this
.
dist
}
/manifest.js`
)
...
...
server/render.js
View file @
737c283b
...
@@ -12,6 +12,7 @@ import { loadGetInitialProps } from '../lib/utils'
...
@@ -12,6 +12,7 @@ import { loadGetInitialProps } from '../lib/utils'
import
Head
,
{
defaultHead
}
from
'../lib/head'
import
Head
,
{
defaultHead
}
from
'../lib/head'
import
App
from
'../lib/app'
import
App
from
'../lib/app'
import
ErrorDebug
from
'../lib/error-debug'
import
ErrorDebug
from
'../lib/error-debug'
import
{
flushChunks
}
from
'../lib/dynamic'
export
async
function
render
(
req
,
res
,
pathname
,
query
,
opts
)
{
export
async
function
render
(
req
,
res
,
pathname
,
query
,
opts
)
{
const
html
=
await
renderToHTML
(
req
,
res
,
pathname
,
opts
)
const
html
=
await
renderToHTML
(
req
,
res
,
pathname
,
opts
)
...
@@ -79,12 +80,13 @@ async function doRender (req, res, pathname, query, {
...
@@ -79,12 +80,13 @@ async function doRender (req, res, pathname, query, {
}
finally
{
}
finally
{
head
=
Head
.
rewind
()
||
defaultHead
()
head
=
Head
.
rewind
()
||
defaultHead
()
}
}
const
chunks
=
flushChunks
()
if
(
err
&&
dev
)
{
if
(
err
&&
dev
)
{
errorHtml
=
render
(
createElement
(
ErrorDebug
,
{
error
:
err
}))
errorHtml
=
render
(
createElement
(
ErrorDebug
,
{
error
:
err
}))
}
}
return
{
html
,
head
,
errorHtml
}
return
{
html
,
head
,
errorHtml
,
chunks
}
}
}
const
docProps
=
await
loadGetInitialProps
(
Document
,
{
...
ctx
,
renderPage
})
const
docProps
=
await
loadGetInitialProps
(
Document
,
{
...
ctx
,
renderPage
})
...
...
test/integration/basic/components/hello1.js
0 → 100644
View file @
737c283b
export
default
()
=>
(
<
p
>
Hello
World
1
<
/p
>
)
test/integration/basic/pages/dynamic/no-ssr-custom-loading.js
0 → 100644
View file @
737c283b
import
dynamic
from
'next/dynamic'
const
Hello
=
dynamic
(
import
(
'../../components/hello1'
),
{
ssr
:
false
,
loading
:
()
=>
(
<
p
>
LOADING
<
/p>
)
})
export
default
Hello
test/integration/basic/pages/dynamic/no-ssr.js
0 → 100644
View file @
737c283b
import
dynamic
from
'next/dynamic'
const
Hello
=
dynamic
(
import
(
'../../components/hello1'
),
{
ssr
:
false
})
export
default
Hello
test/integration/basic/pages/dynamic/ssr.js
0 → 100644
View file @
737c283b
import
dynamic
from
'next/dynamic'
const
Hello
=
dynamic
(
import
(
'../../components/hello1'
))
export
default
Hello
test/integration/basic/test/rendering.js
View file @
737c283b
...
@@ -79,5 +79,20 @@ export default function ({ app }, suiteName, render) {
...
@@ -79,5 +79,20 @@ export default function ({ app }, suiteName, render) {
expect
(
$
(
'h1'
).
text
()).
toBe
(
'404'
)
expect
(
$
(
'h1'
).
text
()).
toBe
(
'404'
)
expect
(
$
(
'h2'
).
text
()).
toBe
(
'This page could not be found.'
)
expect
(
$
(
'h2'
).
text
()).
toBe
(
'This page could not be found.'
)
})
})
test
(
'render dynmaic import components via SSR'
,
async
()
=>
{
const
$
=
await
get
$
(
'/dynamic/ssr'
)
expect
(
$
(
'p'
).
text
()).
toBe
(
'Hello World 1'
)
})
test
(
'stop render dynmaic import components in SSR'
,
async
()
=>
{
const
$
=
await
get
$
(
'/dynamic/no-ssr'
)
expect
(
$
(
'p'
).
text
()).
toBe
(
'loading...'
)
})
test
(
'stop render dynmaic import components in SSR with custom loading'
,
async
()
=>
{
const
$
=
await
get
$
(
'/dynamic/no-ssr-custom-loading'
)
expect
(
$
(
'p'
).
text
()).
toBe
(
'LOADING'
)
})
})
})
}
}
test/integration/static/components/hello.js
0 → 100644
View file @
737c283b
export
default
()
=>
(
<
p
>
Welcome
to
dynamic
imports
.
<
/p
>
)
test/integration/static/next.config.js
View file @
737c283b
...
@@ -4,6 +4,7 @@ module.exports = {
...
@@ -4,6 +4,7 @@ module.exports = {
'/'
:
{
page
:
'/'
},
'/'
:
{
page
:
'/'
},
'/about'
:
{
page
:
'/about'
},
'/about'
:
{
page
:
'/about'
},
'/counter'
:
{
page
:
'/counter'
},
'/counter'
:
{
page
:
'/counter'
},
'/dynamic-imports'
:
{
page
:
'/dynamic-imports'
},
'/dynamic'
:
{
page
:
'/dynamic'
,
query
:
{
text
:
'cool dynamic text'
}
},
'/dynamic'
:
{
page
:
'/dynamic'
,
query
:
{
text
:
'cool dynamic text'
}
},
'/dynamic/one'
:
{
page
:
'/dynamic'
,
query
:
{
text
:
'next export is nice'
}
},
'/dynamic/one'
:
{
page
:
'/dynamic'
,
query
:
{
text
:
'next export is nice'
}
},
'/dynamic/two'
:
{
page
:
'/dynamic'
,
query
:
{
text
:
'zeit is awesome'
}
}
'/dynamic/two'
:
{
page
:
'/dynamic'
,
query
:
{
text
:
'zeit is awesome'
}
}
...
...
test/integration/static/pages/dynamic-imports.js
0 → 100644
View file @
737c283b
import
Link
from
'next/link'
import
dynamic
from
'next/dynamic'
const
DynamicComponent
=
dynamic
(
import
(
'../components/hello'
))
export
default
()
=>
(
<
div
id
=
'dynamic-imports-page'
>
<
div
>
<
Link
href
=
'/'
>
<
a
>
Go
Back
<
/a
>
<
/Link
>
<
/div
>
<
DynamicComponent
/>
<
/div
>
)
test/integration/static/pages/index.js
View file @
737c283b
...
@@ -45,6 +45,9 @@ export default () => (
...
@@ -45,6 +45,9 @@ export default () => (
<
Link
href
=
'/level1/about'
>
<
Link
href
=
'/level1/about'
>
<
a
id
=
'level1-about-page'
>
Level1
about
page
<
/a
>
<
a
id
=
'level1-about-page'
>
Level1
about
page
<
/a
>
<
/Link
>
<
/Link
>
<
Link
href
=
'/dynamic-imports'
>
<
a
id
=
'dynamic-imports-page'
>
Dynamic
imports
page
<
/a
>
<
/Link
>
<
/div
>
<
/div
>
<
p
>
This
is
the
home
page
<
/p
>
<
p
>
This
is
the
home
page
<
/p
>
<
style
jsx
>
{
`
<
style
jsx
>
{
`
...
...
test/integration/static/test/browser.js
View file @
737c283b
...
@@ -93,6 +93,17 @@ export default function (context) {
...
@@ -93,6 +93,17 @@ export default function (context) {
browser
.
close
()
browser
.
close
()
})
})
it
(
'should render dynamic import components in the client'
,
async
()
=>
{
const
browser
=
await
webdriver
(
context
.
port
,
'/'
)
const
text
=
await
browser
.
elementByCss
(
'#dynamic-imports-page'
).
click
()
.
waitForElementByCss
(
'#dynamic-imports-page'
)
.
elementByCss
(
'#dynamic-imports-page p'
).
text
()
expect
(
text
).
toBe
(
'Welcome to dynamic imports.'
)
browser
.
close
()
})
describe
(
'pages in the nested level: level1'
,
()
=>
{
describe
(
'pages in the nested level: level1'
,
()
=>
{
it
(
'should render the home page'
,
async
()
=>
{
it
(
'should render the home page'
,
async
()
=>
{
const
browser
=
await
webdriver
(
context
.
port
,
'/'
)
const
browser
=
await
webdriver
(
context
.
port
,
'/'
)
...
...
test/integration/static/test/ssr.js
View file @
737c283b
...
@@ -17,5 +17,10 @@ export default function (context) {
...
@@ -17,5 +17,10 @@ export default function (context) {
const
html
=
await
renderViaHTTP
(
context
.
port
,
'/dynamic/one'
)
const
html
=
await
renderViaHTTP
(
context
.
port
,
'/dynamic/one'
)
expect
(
html
).
toMatch
(
/next export is nice/
)
expect
(
html
).
toMatch
(
/next export is nice/
)
})
})
it
(
'should render pages with dynamic imports'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
context
.
port
,
'/dynamic-imports'
)
expect
(
html
).
toMatch
(
/Welcome to dynamic imports./
)
})
})
})
}
}
test/unit/same-loop-promise.test.js
0 → 100644
View file @
737c283b
/* global describe, it, expect */
import
{
SameLoopPromise
}
from
'../../dist/lib/dynamic'
describe
(
'SameLoopPromise'
,
()
=>
{
describe
(
'basic api'
,
()
=>
{
it
(
'should support basic promise resolving'
,
(
done
)
=>
{
const
promise
=
new
SameLoopPromise
((
resolve
)
=>
{
setTimeout
(()
=>
{
resolve
(
1000
)
},
100
)
})
promise
.
then
((
value
)
=>
{
expect
(
value
).
toBe
(
1000
)
done
()
})
})
it
(
'should support resolving in the same loop'
,
()
=>
{
let
gotValue
=
null
const
promise
=
new
SameLoopPromise
((
resolve
)
=>
{
resolve
(
1000
)
})
promise
.
then
((
value
)
=>
{
gotValue
=
value
})
expect
(
gotValue
).
toBe
(
1000
)
})
it
(
'should support basic promise rejecting'
,
(
done
)
=>
{
const
error
=
new
Error
(
'Hello Error'
)
const
promise
=
new
SameLoopPromise
((
resolve
,
reject
)
=>
{
setTimeout
(()
=>
{
reject
(
error
)
},
100
)
})
promise
.
catch
((
err
)
=>
{
expect
(
err
).
toBe
(
error
)
done
()
})
})
it
(
'should support rejecting in the same loop'
,
()
=>
{
const
error
=
new
Error
(
'Hello Error'
)
let
gotError
=
null
const
promise
=
new
SameLoopPromise
((
resolve
,
reject
)
=>
{
reject
(
error
)
})
promise
.
catch
((
err
)
=>
{
gotError
=
err
})
expect
(
gotError
).
toBe
(
error
)
})
})
describe
(
'complex usage'
,
()
=>
{
it
(
'should support a chain of promises'
,
(
done
)
=>
{
const
promise
=
new
SameLoopPromise
((
resolve
)
=>
{
setTimeout
(()
=>
{
resolve
(
1000
)
},
100
)
})
promise
.
then
((
value
)
=>
value
*
2
)
.
then
((
value
)
=>
value
+
10
)
.
then
((
value
)
=>
{
expect
(
value
).
toBe
(
2010
)
done
()
})
})
it
(
'should handle the error inside the then'
,
(
done
)
=>
{
const
error
=
new
Error
(
'1000'
)
const
promise
=
new
SameLoopPromise
((
resolve
,
reject
)
=>
{
setTimeout
(()
=>
{
reject
(
error
)
},
100
)
})
promise
.
then
(
()
=>
4000
,
(
err
)
=>
parseInt
(
err
.
message
)
)
.
then
((
value
)
=>
value
+
10
)
.
then
((
value
)
=>
{
expect
(
value
).
toBe
(
1010
)
done
()
})
})
it
(
'should catch the error at the end'
,
(
done
)
=>
{
const
error
=
new
Error
(
'1000'
)
const
promise
=
new
SameLoopPromise
((
resolve
,
reject
)
=>
{
setTimeout
(()
=>
{
reject
(
error
)
},
100
)
})
promise
.
then
((
value
)
=>
value
*
2
)
.
then
((
value
)
=>
value
+
10
)
.
catch
((
err
)
=>
{
expect
(
err
).
toBe
(
error
)
done
()
})
})
it
(
'should catch and proceed'
,
(
done
)
=>
{
const
error
=
new
Error
(
'1000'
)
const
promise
=
new
SameLoopPromise
((
resolve
,
reject
)
=>
{
setTimeout
(()
=>
{
reject
(
error
)
},
100
)
})
promise
.
then
((
value
)
=>
value
*
2
)
.
then
((
value
)
=>
value
+
10
)
.
catch
((
err
)
=>
{
expect
(
err
).
toBe
(
error
)
return
5000
})
.
then
((
value
)
=>
{
expect
(
value
).
toBe
(
5000
)
done
()
})
})
})
})
yarn.lock
View file @
737c283b
This diff is collapsed.
Click to expand it.
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