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
eb4be226
Unverified
Commit
eb4be226
authored
Aug 06, 2020
by
Tom Dohnal
Committed by
GitHub
Aug 06, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
notify component when route hash changes (#13894)
This resolves
https://github.com/vercel/next.js/issues/13659
parent
ebd1434a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
48 deletions
+68
-48
router.ts
packages/next/next-server/lib/router/router.ts
+1
-0
hash-changes.js
test/integration/client-navigation/pages/nav/hash-changes.js
+51
-46
index.test.js
test/integration/client-navigation/test/index.test.js
+16
-2
No files found.
packages/next/next-server/lib/router/router.ts
View file @
eb4be226
...
...
@@ -488,6 +488,7 @@ export default class Router implements BaseRouter {
Router
.
events
.
emit
(
'hashChangeStart'
,
as
)
this
.
changeState
(
method
,
url
,
as
,
options
)
this
.
scrollToHash
(
cleanedAs
)
this
.
notify
(
this
.
components
[
this
.
route
])
Router
.
events
.
emit
(
'hashChangeComplete'
,
as
)
return
true
}
...
...
test/integration/client-navigation/pages/nav/hash-changes.js
View file @
eb4be226
import
React
,
{
Component
}
from
'react'
import
React
from
'react'
import
Link
from
'next/link'
import
{
useRouter
}
from
'next/router'
let
count
=
0
export
default
class
SelfReload
extends
Component
{
static
getInitialProps
({
res
})
{
if
(
res
)
return
{
count
:
0
}
count
+=
1
const
HashChanges
=
({
count
})
=>
{
const
router
=
useRouter
()
return
{
count
}
}
return
(
<
div
id
=
"hash-changes-page"
>
<
Link
href
=
"#via-link"
>
<
a
id
=
"via-link"
>
Via
Link
<
/a
>
<
/Link
>
<
a
href
=
"#via-a"
id
=
"via-a"
>
Via
A
<
/a
>
<
Link
href
=
"/nav/hash-changes"
>
<
a
id
=
"page-url"
>
Page
URL
<
/a
>
<
/Link
>
<
Link
href
=
"#"
>
<
a
id
=
"via-empty-hash"
>
Via
Empty
Hash
<
/a
>
<
/Link
>
<
Link
href
=
"#item-400"
>
<
a
id
=
"scroll-to-item-400"
>
Go
to
item
400
<
/a
>
<
/Link
>
<
Link
href
=
"#name-item-400"
>
<
a
id
=
"scroll-to-name-item-400"
>
Go
to
name
item
400
<
/a
>
<
/Link
>
<
p
>
COUNT
:
{
count
}
<
/p
>
{
Array
.
from
({
length
:
500
},
(
x
,
i
)
=>
i
+
1
).
map
((
i
)
=>
{
return
(
<
div
key
=
{
`item-
${
i
}
`
}
id
=
{
`item-
${
i
}
`
}
>
{
i
}
<
/div
>
)
})}
{
Array
.
from
({
length
:
500
},
(
x
,
i
)
=>
i
+
1
).
map
((
i
)
=>
{
return
(
<
div
key
=
{
`item-
${
i
}
`
}
name
=
{
`name-item-
${
i
}
`
}
>
{
i
}
<
/div
>
)
})}
<
div
id
=
"asPath"
>
ASPATH
:
{
router
.
asPath
}
<
/div
>
<
div
id
=
"pathname"
>
PATHNAME
:
{
router
.
pathname
}
<
/div
>
<
/div
>
)
}
HashChanges
.
getInitialProps
=
({
res
})
=>
{
if
(
res
)
return
{
count
:
0
}
count
+=
1
render
()
{
return
(
<
div
id
=
"hash-changes-page"
>
<
Link
href
=
"#via-link"
>
<
a
id
=
"via-link"
>
Via
Link
<
/a
>
<
/Link
>
<
a
href
=
"#via-a"
id
=
"via-a"
>
Via
A
<
/a
>
<
Link
href
=
"/nav/hash-changes"
>
<
a
id
=
"page-url"
>
Page
URL
<
/a
>
<
/Link
>
<
Link
href
=
"#"
>
<
a
id
=
"via-empty-hash"
>
Via
Empty
Hash
<
/a
>
<
/Link
>
<
Link
href
=
"#item-400"
>
<
a
id
=
"scroll-to-item-400"
>
Go
to
item
400
<
/a
>
<
/Link
>
<
Link
href
=
"#name-item-400"
>
<
a
id
=
"scroll-to-name-item-400"
>
Go
to
name
item
400
<
/a
>
<
/Link
>
<
p
>
COUNT
:
{
this
.
props
.
count
}
<
/p
>
{
Array
.
from
({
length
:
500
},
(
x
,
i
)
=>
i
+
1
).
map
((
i
)
=>
{
return
(
<
div
key
=
{
`item-
${
i
}
`
}
id
=
{
`item-
${
i
}
`
}
>
{
i
}
<
/div
>
)
})}
{
Array
.
from
({
length
:
500
},
(
x
,
i
)
=>
i
+
1
).
map
((
i
)
=>
{
return
(
<
div
key
=
{
`item-
${
i
}
`
}
name
=
{
`name-item-
${
i
}
`
}
>
{
i
}
<
/div
>
)
})}
<
/div
>
)
}
return
{
count
}
}
export
default
HashChanges
test/integration/client-navigation/test/index.test.js
View file @
eb4be226
...
...
@@ -497,8 +497,6 @@ describe('Client Navigation', () => {
.
click
()
.
eval
(
'window.pageYOffset'
)
console
.
log
(
scrollPosition
)
expect
(
scrollPosition
).
toBe
(
16258
)
// Scrolls back to top when scrolling to `#` with no value.
...
...
@@ -534,6 +532,22 @@ describe('Client Navigation', () => {
}
}
})
it
(
'Should update asPath'
,
async
()
=>
{
let
browser
try
{
browser
=
await
webdriver
(
context
.
appPort
,
'/nav/hash-changes'
)
await
browser
.
elementByCss
(
'#via-link'
).
click
()
const
asPath
=
await
browser
.
elementByCss
(
'div#asPath'
).
text
()
expect
(
asPath
).
toBe
(
'ASPATH: /nav/hash-changes#via-link'
)
}
finally
{
if
(
browser
)
{
await
browser
.
close
()
}
}
})
})
describe
(
'when hash change via A tag'
,
()
=>
{
...
...
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