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
fcfbceaa
Unverified
Commit
fcfbceaa
authored
Jul 21, 2020
by
Jan Potoms
Committed by
GitHub
Jul 21, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix cancellation control flow (#15361)
parent
ef637b38
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
143 additions
and
160 deletions
+143
-160
router.ts
packages/next/next-server/lib/router/router.ts
+141
-158
index.test.js
test/integration/build-output/test/index.test.js
+1
-1
index.test.js
test/integration/size-limit/test/index.test.js
+1
-1
No files found.
packages/next/next-server/lib/router/router.ts
View file @
fcfbceaa
...
...
@@ -22,10 +22,14 @@ import {
normalizePathTrailingSlash
,
}
from
'../../../client/normalize-trailing-slash'
const
ABORTED
=
Symbol
(
'aborted'
)
const
basePath
=
(
process
.
env
.
__NEXT_ROUTER_BASEPATH
as
string
)
||
''
function
buildCancellationError
()
{
return
Object
.
assign
(
new
Error
(
'Route Cancelled'
),
{
cancelled
:
true
,
})
}
export
function
addBasePath
(
path
:
string
):
string
{
return
basePath
?
path
===
'/'
...
...
@@ -525,50 +529,47 @@ export default class Router implements BaseRouter {
Router
.
events
.
emit
(
'routeChangeStart'
,
as
)
// If shallow is true and the route exists in the router cache we reuse the previous result
return
this
.
getRouteInfo
(
route
,
pathname
,
query
,
as
,
shallow
).
then
(
(
routeInfo
)
=>
{
const
{
error
}
=
routeInfo
try
{
const
routeInfo
=
await
this
.
getRouteInfo
(
route
,
pathname
,
query
,
as
,
shallow
)
const
{
error
}
=
routeInfo
if
(
error
&&
error
.
cancelled
)
{
// An event already has been fired
return
false
}
Router
.
events
.
emit
(
'beforeHistoryChange'
,
as
)
this
.
changeState
(
method
,
url
,
as
,
options
)
if
(
error
&&
error
[
ABORTED
])
{
Router
.
events
.
emit
(
'routeChangeError'
,
error
,
as
)
return
false
}
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
const
appComp
:
any
=
this
.
components
[
'/_app'
].
Component
;(
window
as
any
).
next
.
isPrerendered
=
appComp
.
getInitialProps
===
appComp
.
origGetInitialProps
&&
!
(
routeInfo
.
Component
as
any
).
getInitialProps
}
Router
.
events
.
emit
(
'beforeHistoryChange'
,
as
)
this
.
changeState
(
method
,
url
,
as
,
options
)
await
this
.
set
(
route
,
pathname
!
,
query
,
cleanedAs
,
routeInfo
)
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
const
appComp
:
any
=
this
.
components
[
'/_app'
].
Component
;(
window
as
any
).
next
.
isPrerendered
=
appComp
.
getInitialProps
===
appComp
.
origGetInitialProps
&&
!
(
routeInfo
.
Component
as
any
).
getInitialProps
if
(
error
)
{
Router
.
events
.
emit
(
'routeChangeError'
,
error
,
cleanedAs
)
throw
error
}
if
(
process
.
env
.
__NEXT_SCROLL_RESTORATION
)
{
if
(
manualScrollRestoration
&&
'_N_X'
in
options
)
{
window
.
scrollTo
(
options
.
_N_X
,
options
.
_N_Y
)
}
}
Router
.
events
.
emit
(
'routeChangeComplete'
,
as
)
return
this
.
set
(
route
,
pathname
!
,
query
,
cleanedAs
,
routeInfo
).
then
(
()
=>
{
if
(
error
)
{
Router
.
events
.
emit
(
'routeChangeError'
,
error
,
cleanedAs
)
throw
error
}
if
(
process
.
env
.
__NEXT_SCROLL_RESTORATION
)
{
if
(
manualScrollRestoration
&&
'_N_X'
in
options
)
{
window
.
scrollTo
(
options
.
_N_X
,
options
.
_N_Y
)
}
}
Router
.
events
.
emit
(
'routeChangeComplete'
,
as
)
return
true
}
)
return
true
}
catch
(
err
)
{
if
(
err
.
cancelled
)
{
return
false
}
)
throw
err
}
}
changeState
(
...
...
@@ -605,138 +606,122 @@ export default class Router implements BaseRouter {
}
}
getRouteInfo
(
route
:
string
,
async
handleRouteInfoError
(
err
:
Error
&
{
code
:
any
;
cancelled
:
boolean
}
,
pathname
:
string
,
query
:
any
,
as
:
string
,
shallow
:
boolean
=
false
loadErrorFail
?:
boolean
):
Promise
<
RouteInfo
>
{
const
cachedRouteInfo
=
this
.
components
[
route
]
// If there is a shallow route transition possible
// If the route is already rendered on the screen.
if
(
shallow
&&
cachedRouteInfo
&&
this
.
route
===
route
)
{
return
Promise
.
resolve
(
cachedRouteInfo
)
}
const
handleError
=
(
err
:
Error
&
{
code
:
any
;
cancelled
:
boolean
;
[
ABORTED
]:
boolean
},
loadErrorFail
?:
boolean
)
=>
{
return
new
Promise
((
resolve
)
=>
{
if
(
err
.
code
===
'PAGE_LOAD_ERROR'
||
loadErrorFail
)
{
// If we can't load the page it could be one of following reasons
// 1. Page doesn't exists
// 2. Page does exist in a different zone
// 3. Internal error while loading the page
// So, doing a hard reload is the proper way to deal with this.
window
.
location
.
href
=
as
// Changing the URL doesn't block executing the current code path.
// So, we need to mark it as aborted and stop the routing logic.
err
[
ABORTED
]
=
true
// @ts-ignore TODO: fix the control flow here
return
resolve
({
error
:
err
})
}
if
(
err
.
cancelled
)
{
// bubble up cancellation errors
throw
err
}
if
(
err
.
cancelled
)
{
// @ts-ignore TODO: fix the control flow here
return
resolve
({
error
:
err
})
}
if
(
err
.
code
===
'PAGE_LOAD_ERROR'
||
loadErrorFail
)
{
Router
.
events
.
emit
(
'routeChangeError'
,
err
,
as
)
resolve
(
this
.
fetchComponent
(
'/_error'
)
.
then
((
res
)
=>
{
const
{
page
:
Component
}
=
res
const
routeInfo
:
RouteInfo
=
{
Component
,
err
}
return
new
Promise
((
resolveRouteInfo
)
=>
{
this
.
getInitialProps
(
Component
,
{
err
,
pathname
,
query
,
}
as
any
).
then
(
(
props
)
=>
{
routeInfo
.
props
=
props
routeInfo
.
error
=
err
resolveRouteInfo
(
routeInfo
)
},
(
gipErr
)
=>
{
console
.
error
(
'Error in error page `getInitialProps`: '
,
gipErr
)
routeInfo
.
error
=
err
routeInfo
.
props
=
{}
resolveRouteInfo
(
routeInfo
)
}
)
})
as
Promise
<
RouteInfo
>
})
.
catch
((
routeInfoErr
)
=>
handleError
(
routeInfoErr
,
true
))
)
})
as
Promise
<
RouteInfo
>
// If we can't load the page it could be one of following reasons
// 1. Page doesn't exists
// 2. Page does exist in a different zone
// 3. Internal error while loading the page
// So, doing a hard reload is the proper way to deal with this.
window
.
location
.
href
=
as
// Changing the URL doesn't block executing the current code path.
// So let's throw a cancellation error stop the routing logic.
throw
buildCancellationError
()
}
return
(
new
Promise
((
resolve
,
reject
)
=>
{
if
(
cachedRouteInfo
)
{
return
resolve
(
cachedRouteInfo
)
try
{
const
{
page
:
Component
}
=
await
this
.
fetchComponent
(
'/_error'
)
const
routeInfo
:
RouteInfo
=
{
Component
,
err
,
error
:
err
}
try
{
routeInfo
.
props
=
await
this
.
getInitialProps
(
Component
,
{
err
,
pathname
,
query
,
}
as
any
)
}
catch
(
gipErr
)
{
console
.
error
(
'Error in error page `getInitialProps`: '
,
gipErr
)
routeInfo
.
props
=
{}
}
this
.
fetchComponent
(
route
).
then
(
(
res
)
=>
resolve
({
Component
:
res
.
page
,
__N_SSG
:
res
.
mod
.
__N_SSG
,
__N_SSP
:
res
.
mod
.
__N_SSP
,
}),
reject
)
})
as
Promise
<
RouteInfo
>
)
.
then
((
routeInfo
:
RouteInfo
)
=>
{
const
{
Component
,
__N_SSG
,
__N_SSP
}
=
routeInfo
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
const
{
isValidElementType
}
=
require
(
'react-is'
)
if
(
!
isValidElementType
(
Component
))
{
throw
new
Error
(
`The default export is not a React Component in page: "
${
pathname
}
"`
)
}
}
return
routeInfo
}
catch
(
routeInfoErr
)
{
return
this
.
handleRouteInfoError
(
routeInfoErr
,
pathname
,
query
,
as
,
true
)
}
}
async
getRouteInfo
(
route
:
string
,
pathname
:
string
,
query
:
any
,
as
:
string
,
shallow
:
boolean
=
false
):
Promise
<
RouteInfo
>
{
try
{
const
cachedRouteInfo
=
this
.
components
[
route
]
if
(
shallow
&&
cachedRouteInfo
&&
this
.
route
===
route
)
{
return
cachedRouteInfo
}
let
dataHref
:
string
|
undefined
const
routeInfo
=
cachedRouteInfo
?
cachedRouteInfo
:
await
this
.
fetchComponent
(
route
).
then
(
(
res
)
=>
({
Component
:
res
.
page
,
__N_SSG
:
res
.
mod
.
__N_SSG
,
__N_SSP
:
res
.
mod
.
__N_SSP
,
}
as
RouteInfo
)
)
const
{
Component
,
__N_SSG
,
__N_SSP
}
=
routeInfo
if
(
__N_SSG
||
__N_SSP
)
{
dataHref
=
this
.
pageLoader
.
getDataHref
(
formatWithValidation
({
pathname
,
query
}),
as
,
__N_SSG
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
const
{
isValidElementType
}
=
require
(
'react-is'
)
if
(
!
isValidElementType
(
Component
))
{
throw
new
Error
(
`The default export is not a React Component in page: "
${
pathname
}
"`
)
}
}
let
dataHref
:
string
|
undefined
return
this
.
_getData
<
RouteInfo
>
(()
=>
if
(
__N_SSG
||
__N_SSP
)
{
dataHref
=
this
.
pageLoader
.
getDataHref
(
formatWithValidation
({
pathname
,
query
}),
as
,
__N_SSG
?
this
.
_getStaticData
(
dataHref
!
)
:
__N_SSP
?
this
.
_getServerData
(
dataHref
!
)
:
this
.
getInitialProps
(
Component
,
// we provide AppTree later so this needs to be `any`
{
pathname
,
query
,
asPath
:
as
,
}
as
any
)
).
then
((
props
)
=>
{
routeInfo
.
props
=
props
this
.
components
[
route
]
=
routeInfo
return
routeInfo
})
})
.
catch
(
handleError
)
)
}
const
props
=
await
this
.
_getData
<
RouteInfo
>
(()
=>
__N_SSG
?
this
.
_getStaticData
(
dataHref
!
)
:
__N_SSP
?
this
.
_getServerData
(
dataHref
!
)
:
this
.
getInitialProps
(
Component
,
// we provide AppTree later so this needs to be `any`
{
pathname
,
query
,
asPath
:
as
,
}
as
any
)
)
routeInfo
.
props
=
props
this
.
components
[
route
]
=
routeInfo
return
routeInfo
}
catch
(
err
)
{
return
this
.
handleRouteInfoError
(
err
,
pathname
,
query
,
as
)
}
}
set
(
...
...
@@ -917,9 +902,7 @@ export default class Router implements BaseRouter {
abortComponentLoad
(
as
:
string
):
void
{
if
(
this
.
clc
)
{
const
e
=
new
Error
(
'Route Cancelled'
)
;(
e
as
any
).
cancelled
=
true
Router
.
events
.
emit
(
'routeChangeError'
,
e
,
as
)
Router
.
events
.
emit
(
'routeChangeError'
,
buildCancellationError
(),
as
)
this
.
clc
()
this
.
clc
=
null
}
...
...
test/integration/build-output/test/index.test.js
View file @
fcfbceaa
...
...
@@ -104,7 +104,7 @@ describe('Build Output', () => {
expect
(
parseFloat
(
err404FirstLoad
)
-
63
).
toBeLessThanOrEqual
(
0
)
expect
(
err404FirstLoad
.
endsWith
(
'kB'
)).
toBe
(
true
)
expect
(
parseFloat
(
sharedByAll
)
-
59
).
toBeLessThanOrEqual
(
0
)
expect
(
parseFloat
(
sharedByAll
)
-
59
.2
).
toBeLessThanOrEqual
(
0
)
expect
(
sharedByAll
.
endsWith
(
'kB'
)).
toBe
(
true
)
if
(
_appSize
.
endsWith
(
'kB'
))
{
...
...
test/integration/size-limit/test/index.test.js
View file @
fcfbceaa
...
...
@@ -80,7 +80,7 @@ describe('Production response size', () => {
)
// These numbers are without gzip compression!
const
delta
=
responseSizesBytes
-
27
3
*
1024
const
delta
=
responseSizesBytes
-
27
4
*
1024
expect
(
delta
).
toBeLessThanOrEqual
(
1024
)
// don't increase size more than 1kb
expect
(
delta
).
toBeGreaterThanOrEqual
(
-
1024
)
// don't decrease size more than 1kb without updating target
})
...
...
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