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
841d4efc
Unverified
Commit
841d4efc
authored
Aug 01, 2020
by
Jan Potoms
Committed by
GitHub
Aug 01, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve router types (#15775)
parent
d0c57cf6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
16 deletions
+31
-16
router.ts
packages/next/next-server/lib/router/router.ts
+31
-16
No files found.
packages/next/next-server/lib/router/router.ts
View file @
841d4efc
...
@@ -22,6 +22,18 @@ import {
...
@@ -22,6 +22,18 @@ import {
normalizePathTrailingSlash
,
normalizePathTrailingSlash
,
}
from
'../../../client/normalize-trailing-slash'
}
from
'../../../client/normalize-trailing-slash'
interface
TransitionOptions
{
shallow
?:
boolean
}
interface
NextHistoryState
{
url
:
string
as
:
string
options
:
TransitionOptions
}
type
HistoryState
=
null
|
{
__N
:
false
}
|
({
__N
:
true
}
&
NextHistoryState
)
const
basePath
=
(
process
.
env
.
__NEXT_ROUTER_BASEPATH
as
string
)
||
''
const
basePath
=
(
process
.
env
.
__NEXT_ROUTER_BASEPATH
as
string
)
||
''
function
buildCancellationError
()
{
function
buildCancellationError
()
{
...
@@ -127,7 +139,7 @@ type RouteInfo = {
...
@@ -127,7 +139,7 @@ type RouteInfo = {
type
Subscription
=
(
data
:
RouteInfo
,
App
?:
ComponentType
)
=>
Promise
<
void
>
type
Subscription
=
(
data
:
RouteInfo
,
App
?:
ComponentType
)
=>
Promise
<
void
>
type
BeforePopStateCallback
=
(
state
:
any
)
=>
boolean
type
BeforePopStateCallback
=
(
state
:
NextHistoryState
)
=>
boolean
type
ComponentLoadCancel
=
(()
=>
void
)
|
null
type
ComponentLoadCancel
=
(()
=>
void
)
|
null
...
@@ -325,7 +337,9 @@ export default class Router implements BaseRouter {
...
@@ -325,7 +337,9 @@ export default class Router implements BaseRouter {
}
}
onPopState
=
(
e
:
PopStateEvent
):
void
=>
{
onPopState
=
(
e
:
PopStateEvent
):
void
=>
{
if
(
!
e
.
state
)
{
const
state
=
e
.
state
as
HistoryState
if
(
!
state
)
{
// We get state as undefined for two reasons.
// We get state as undefined for two reasons.
// 1. With older safari (< 8) and older chrome (< 34)
// 1. With older safari (< 8) and older chrome (< 34)
// 2. When the URL changed with #
// 2. When the URL changed with #
...
@@ -344,11 +358,12 @@ export default class Router implements BaseRouter {
...
@@ -344,11 +358,12 @@ export default class Router implements BaseRouter {
return
return
}
}
const
{
url
,
as
,
options
,
__N
}
=
e
.
state
if
(
!
state
.
__N
)
{
if
(
!
__N
)
{
// this history state wasn't created by next.js so it can be ignored
return
return
}
}
const
{
url
,
as
,
options
}
=
state
const
{
pathname
}
=
parseRelativeUrl
(
url
)
const
{
pathname
}
=
parseRelativeUrl
(
url
)
// Make sure we don't re-render on initial load,
// Make sure we don't re-render on initial load,
...
@@ -359,7 +374,7 @@ export default class Router implements BaseRouter {
...
@@ -359,7 +374,7 @@ export default class Router implements BaseRouter {
// If the downstream application returns falsy, return.
// If the downstream application returns falsy, return.
// They will then be responsible for handling the event.
// They will then be responsible for handling the event.
if
(
this
.
_bps
&&
!
this
.
_bps
(
e
.
state
))
{
if
(
this
.
_bps
&&
!
this
.
_bps
(
state
))
{
return
return
}
}
...
@@ -408,7 +423,7 @@ export default class Router implements BaseRouter {
...
@@ -408,7 +423,7 @@ export default class Router implements BaseRouter {
* @param as masks `url` for the browser
* @param as masks `url` for the browser
* @param options object you can define `shallow` and other options
* @param options object you can define `shallow` and other options
*/
*/
push
(
url
:
Url
,
as
:
Url
=
url
,
options
=
{})
{
push
(
url
:
Url
,
as
:
Url
=
url
,
options
:
TransitionOptions
=
{})
{
;({
url
,
as
}
=
prepareUrlAs
(
this
,
url
,
as
))
;({
url
,
as
}
=
prepareUrlAs
(
this
,
url
,
as
))
return
this
.
change
(
'pushState'
,
url
,
as
,
options
)
return
this
.
change
(
'pushState'
,
url
,
as
,
options
)
}
}
...
@@ -419,7 +434,7 @@ export default class Router implements BaseRouter {
...
@@ -419,7 +434,7 @@ export default class Router implements BaseRouter {
* @param as masks `url` for the browser
* @param as masks `url` for the browser
* @param options object you can define `shallow` and other options
* @param options object you can define `shallow` and other options
*/
*/
replace
(
url
:
Url
,
as
:
Url
=
url
,
options
=
{})
{
replace
(
url
:
Url
,
as
:
Url
=
url
,
options
:
TransitionOptions
=
{})
{
;({
url
,
as
}
=
prepareUrlAs
(
this
,
url
,
as
))
;({
url
,
as
}
=
prepareUrlAs
(
this
,
url
,
as
))
return
this
.
change
(
'replaceState'
,
url
,
as
,
options
)
return
this
.
change
(
'replaceState'
,
url
,
as
,
options
)
}
}
...
@@ -428,9 +443,9 @@ export default class Router implements BaseRouter {
...
@@ -428,9 +443,9 @@ export default class Router implements BaseRouter {
method
:
HistoryMethod
,
method
:
HistoryMethod
,
url
:
string
,
url
:
string
,
as
:
string
,
as
:
string
,
options
:
any
options
:
TransitionOptions
):
Promise
<
boolean
>
{
):
Promise
<
boolean
>
{
if
(
!
options
.
_h
)
{
if
(
!
(
options
as
any
)
.
_h
)
{
this
.
isSsr
=
false
this
.
isSsr
=
false
}
}
// marking route changes as a navigation start entry
// marking route changes as a navigation start entry
...
@@ -462,7 +477,7 @@ export default class Router implements BaseRouter {
...
@@ -462,7 +477,7 @@ export default class Router implements BaseRouter {
// WARNING: `_h` is an internal option for handing Next.js client-side
// WARNING: `_h` is an internal option for handing Next.js client-side
// hydration. Your app should _never_ use this property. It may change at
// hydration. Your app should _never_ use this property. It may change at
// any time without notice.
// any time without notice.
if
(
!
options
.
_h
&&
this
.
onlyAHashChange
(
cleanedAs
))
{
if
(
!
(
options
as
any
)
.
_h
&&
this
.
onlyAHashChange
(
cleanedAs
))
{
this
.
asPath
=
cleanedAs
this
.
asPath
=
cleanedAs
Router
.
events
.
emit
(
'hashChangeStart'
,
as
)
Router
.
events
.
emit
(
'hashChangeStart'
,
as
)
this
.
changeState
(
method
,
url
,
as
,
options
)
this
.
changeState
(
method
,
url
,
as
,
options
)
...
@@ -558,7 +573,7 @@ export default class Router implements BaseRouter {
...
@@ -558,7 +573,7 @@ export default class Router implements BaseRouter {
if
(
process
.
env
.
__NEXT_SCROLL_RESTORATION
)
{
if
(
process
.
env
.
__NEXT_SCROLL_RESTORATION
)
{
if
(
manualScrollRestoration
&&
'_N_X'
in
options
)
{
if
(
manualScrollRestoration
&&
'_N_X'
in
options
)
{
window
.
scrollTo
(
options
.
_N_X
,
options
.
_N_Y
)
window
.
scrollTo
(
(
options
as
any
).
_N_X
,
(
options
as
any
)
.
_N_Y
)
}
}
}
}
Router
.
events
.
emit
(
'routeChangeComplete'
,
as
)
Router
.
events
.
emit
(
'routeChangeComplete'
,
as
)
...
@@ -576,7 +591,7 @@ export default class Router implements BaseRouter {
...
@@ -576,7 +591,7 @@ export default class Router implements BaseRouter {
method
:
HistoryMethod
,
method
:
HistoryMethod
,
url
:
string
,
url
:
string
,
as
:
string
,
as
:
string
,
options
=
{}
options
:
TransitionOptions
=
{}
):
void
{
):
void
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
typeof
window
.
history
===
'undefined'
)
{
if
(
typeof
window
.
history
===
'undefined'
)
{
...
@@ -597,7 +612,7 @@ export default class Router implements BaseRouter {
...
@@ -597,7 +612,7 @@ export default class Router implements BaseRouter {
as
,
as
,
options
,
options
,
__N
:
true
,
__N
:
true
,
},
}
as
HistoryState
,
// Most browsers currently ignores this parameter, although they may use it in the future.
// Most browsers currently ignores this parameter, although they may use it in the future.
// Passing the empty string here should be safe against future changes to the method.
// Passing the empty string here should be safe against future changes to the method.
// https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState
// https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState
...
@@ -610,7 +625,7 @@ export default class Router implements BaseRouter {
...
@@ -610,7 +625,7 @@ export default class Router implements BaseRouter {
async
handleRouteInfoError
(
async
handleRouteInfoError
(
err
:
Error
&
{
code
:
any
;
cancelled
:
boolean
},
err
:
Error
&
{
code
:
any
;
cancelled
:
boolean
},
pathname
:
string
,
pathname
:
string
,
query
:
an
y
,
query
:
ParsedUrlQuer
y
,
as
:
string
,
as
:
string
,
loadErrorFail
?:
boolean
loadErrorFail
?:
boolean
):
Promise
<
RouteInfo
>
{
):
Promise
<
RouteInfo
>
{
...
@@ -728,7 +743,7 @@ export default class Router implements BaseRouter {
...
@@ -728,7 +743,7 @@ export default class Router implements BaseRouter {
set
(
set
(
route
:
string
,
route
:
string
,
pathname
:
string
,
pathname
:
string
,
query
:
an
y
,
query
:
ParsedUrlQuer
y
,
as
:
string
,
as
:
string
,
data
:
RouteInfo
data
:
RouteInfo
):
Promise
<
void
>
{
):
Promise
<
void
>
{
...
...
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