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
3cd45bc3
Unverified
Commit
3cd45bc3
authored
Feb 20, 2020
by
JJ Kasper
Committed by
GitHub
Feb 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make sure to handle failing to load _error (#10617)
parent
90480b84
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
111 additions
and
52 deletions
+111
-52
router.ts
packages/next/next-server/lib/router/router.ts
+59
-52
broken.js
test/integration/error-load-fail/pages/broken.js
+7
-0
index.js
test/integration/error-load-fail/pages/index.js
+9
-0
index.test.js
test/integration/error-load-fail/test/index.test.js
+36
-0
No files found.
packages/next/next-server/lib/router/router.ts
View file @
3cd45bc3
...
...
@@ -500,6 +500,64 @@ export default class Router implements BaseRouter {
return
Promise
.
resolve
(
cachedRouteInfo
)
}
const
handleError
=
(
err
:
Error
&
{
code
:
any
;
cancelled
:
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 a cancelled error and stop the routing logic.
err
.
cancelled
=
true
// @ts-ignore TODO: fix the control flow here
return
resolve
({
error
:
err
})
}
if
(
err
.
cancelled
)
{
// @ts-ignore TODO: fix the control flow here
return
resolve
({
error
:
err
})
}
resolve
(
this
.
fetchComponent
(
'/_error'
)
.
then
(
Component
=>
{
const
routeInfo
:
RouteInfo
=
{
Component
,
err
}
return
new
Promise
(
resolve
=>
{
this
.
getInitialProps
(
Component
,
{
err
,
pathname
,
query
,
}
as
any
).
then
(
props
=>
{
routeInfo
.
props
=
props
routeInfo
.
error
=
err
resolve
(
routeInfo
)
},
gipErr
=>
{
console
.
error
(
'Error in error page `getInitialProps`: '
,
gipErr
)
routeInfo
.
error
=
err
routeInfo
.
props
=
{}
resolve
(
routeInfo
)
}
)
})
as
Promise
<
RouteInfo
>
})
.
catch
(
err
=>
handleError
(
err
,
true
))
)
})
as
Promise
<
RouteInfo
>
}
return
(
new
Promise
((
resolve
,
reject
)
=>
{
if
(
cachedRouteInfo
)
{
return
resolve
(
cachedRouteInfo
)
...
...
@@ -542,58 +600,7 @@ export default class Router implements BaseRouter {
return
routeInfo
})
})
.
catch
(
err
=>
{
return
new
Promise
(
resolve
=>
{
if
(
err
.
code
===
'PAGE_LOAD_ERROR'
)
{
// 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 a cancelled error and stop the routing logic.
err
.
cancelled
=
true
// @ts-ignore TODO: fix the control flow here
return
resolve
({
error
:
err
})
}
if
(
err
.
cancelled
)
{
// @ts-ignore TODO: fix the control flow here
return
resolve
({
error
:
err
})
}
resolve
(
this
.
fetchComponent
(
'/_error'
).
then
(
Component
=>
{
const
routeInfo
:
RouteInfo
=
{
Component
,
err
}
return
new
Promise
(
resolve
=>
{
this
.
getInitialProps
(
Component
,
{
err
,
pathname
,
query
,
}
as
any
).
then
(
props
=>
{
routeInfo
.
props
=
props
routeInfo
.
error
=
err
resolve
(
routeInfo
)
},
gipErr
=>
{
console
.
error
(
'Error in error page `getInitialProps`: '
,
gipErr
)
routeInfo
.
error
=
err
routeInfo
.
props
=
{}
resolve
(
routeInfo
)
}
)
})
as
Promise
<
RouteInfo
>
})
)
})
as
Promise
<
RouteInfo
>
})
.
catch
(
handleError
)
}
set
(
...
...
test/integration/error-load-fail/pages/broken.js
0 → 100644
View file @
3cd45bc3
const
Page
=
()
=>
'oops'
Page
.
getInitialProps
=
()
=>
{
throw
new
Error
(
'oops'
)
}
export
default
Page
test/integration/error-load-fail/pages/index.js
0 → 100644
View file @
3cd45bc3
import
Link
from
'next/link'
export
default
()
=>
(
<>
<
Link
href
=
"/broken"
>
<
a
id
=
"to-broken"
>
to
oops
<
/a
>
<
/Link
>
<
/
>
)
test/integration/error-load-fail/test/index.test.js
0 → 100644
View file @
3cd45bc3
/* eslint-env jest */
/* global jasmine */
import
path
from
'path'
import
webdriver
from
'next-webdriver'
import
{
nextBuild
,
nextStart
,
findPort
,
killApp
,
waitFor
,
}
from
'next-test-utils'
jasmine
.
DEFAULT_TIMEOUT_INTERVAL
=
1000
*
60
*
1
const
appDir
=
path
.
join
(
__dirname
,
'..'
)
describe
(
'Failing to load _error'
,
()
=>
{
it
(
'handles failing to load _error correctly'
,
async
()
=>
{
await
nextBuild
(
appDir
)
const
appPort
=
await
findPort
()
const
app
=
await
nextStart
(
appDir
,
appPort
)
const
browser
=
await
webdriver
(
appPort
,
'/'
)
await
browser
.
eval
(
`window.beforeNavigate = true`
)
await
browser
.
elementByCss
(
'#to-broken'
).
moveTo
()
await
browser
.
waitForElementByCss
(
'script[src*="broken.js"'
)
// stop app so that _error can't be loaded
await
killApp
(
app
)
await
browser
.
elementByCss
(
'#to-broken'
).
click
()
await
waitFor
(
2000
)
expect
(
await
browser
.
eval
(
'window.beforeNavigate'
)).
toBeFalsy
()
})
})
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