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
23295ef3
Unverified
Commit
23295ef3
authored
Jun 08, 2022
by
Shu Ding
Committed by
GitHub
Jun 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Avoid unnecessary router state changes" (#37572)
Reverts vercel/next.js#37431
parent
748c8e80
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
146 deletions
+28
-146
router.ts
packages/next/shared/lib/router/router.ts
+28
-68
next.config.js
test/integration/rewrites-client-rerender/next.config.js
+0
-10
index.js
test/integration/rewrites-client-rerender/pages/index.js
+0
-13
index.test.js
test/integration/rewrites-client-rerender/test/index.test.js
+0
-55
No files found.
packages/next/shared/lib/router/router.ts
View file @
23295ef3
...
@@ -81,37 +81,6 @@ function buildCancellationError() {
...
@@ -81,37 +81,6 @@ function buildCancellationError() {
})
})
}
}
function
compareRouterStates
(
a
:
Router
[
'state'
],
b
:
Router
[
'state'
])
{
const
stateKeys
=
Object
.
keys
(
a
)
if
(
stateKeys
.
length
!==
Object
.
keys
(
b
).
length
)
return
false
for
(
let
i
=
stateKeys
.
length
;
i
--
;
)
{
const
key
=
stateKeys
[
i
]
if
(
key
===
'query'
)
{
const
queryKeys
=
Object
.
keys
(
a
.
query
)
if
(
queryKeys
.
length
!==
Object
.
keys
(
b
.
query
).
length
)
{
return
false
}
for
(
let
j
=
queryKeys
.
length
;
j
--
;
)
{
const
queryKey
=
queryKeys
[
j
]
if
(
!
b
.
query
.
hasOwnProperty
(
queryKey
)
||
a
.
query
[
queryKey
]
!==
b
.
query
[
queryKey
]
)
{
return
false
}
}
}
else
if
(
!
b
.
hasOwnProperty
(
key
)
||
a
[
key
as
keyof
Router
[
'state'
]]
!==
b
[
key
as
keyof
Router
[
'state'
]]
)
{
return
false
}
}
return
true
}
/**
/**
* Detects whether a given url is routable by the Next.js router (browser only).
* Detects whether a given url is routable by the Next.js router (browser only).
*/
*/
...
@@ -1343,47 +1312,38 @@ export default class Router implements BaseRouter {
...
@@ -1343,47 +1312,38 @@ export default class Router implements BaseRouter {
const
shouldScroll
=
options
.
scroll
??
!
isValidShallowRoute
const
shouldScroll
=
options
.
scroll
??
!
isValidShallowRoute
const
resetScroll
=
shouldScroll
?
{
x
:
0
,
y
:
0
}
:
null
const
resetScroll
=
shouldScroll
?
{
x
:
0
,
y
:
0
}
:
null
const
nextScroll
=
forcedScroll
??
resetScroll
await
this
.
set
(
const
mergedNextState
=
{
{
...
nextState
,
...
nextState
,
route
,
route
,
pathname
,
pathname
,
query
,
query
,
asPath
:
cleanedAs
,
asPath
:
cleanedAs
,
isFallback
:
false
,
isFallback
:
false
,
}
},
routeInfo
,
// for query updates we can skip it if the state is unchanged and we don't
forcedScroll
??
resetScroll
// need to scroll
).
catch
((
e
)
=>
{
// https://github.com/vercel/next.js/issues/37139
if
(
e
.
cancelled
)
error
=
error
||
e
const
canSkipUpdating
=
else
throw
e
(
options
as
any
).
_h
&&
})
!
nextScroll
&&
compareRouterStates
(
mergedNextState
,
this
.
state
)
if
(
!
canSkipUpdating
)
{
await
this
.
set
(
mergedNextState
,
routeInfo
,
nextScroll
).
catch
((
e
)
=>
{
if
(
e
.
cancelled
)
error
=
error
||
e
else
throw
e
})
if
(
error
)
{
if
(
error
)
{
Router
.
events
.
emit
(
'routeChangeError'
,
error
,
cleanedAs
,
routeProps
)
Router
.
events
.
emit
(
'routeChangeError'
,
error
,
cleanedAs
,
routeProps
)
throw
error
throw
error
}
}
if
(
process
.
env
.
__NEXT_I18N_SUPPORT
)
{
if
(
process
.
env
.
__NEXT_I18N_SUPPORT
)
{
if
(
nextState
.
locale
)
{
if
(
nextState
.
locale
)
{
document
.
documentElement
.
lang
=
nextState
.
locale
document
.
documentElement
.
lang
=
nextState
.
locale
}
}
}
Router
.
events
.
emit
(
'routeChangeComplete'
,
as
,
routeProps
)
}
Router
.
events
.
emit
(
'routeChangeComplete'
,
as
,
routeProps
)
// A hash mark # is the optional last part of a URL
// A hash mark # is the optional last part of a URL
const
hashRegex
=
/#.+$/
const
hashRegex
=
/#.+$/
if
(
shouldScroll
&&
hashRegex
.
test
(
as
))
{
if
(
shouldScroll
&&
hashRegex
.
test
(
as
))
{
this
.
scrollToHash
(
as
)
this
.
scrollToHash
(
as
)
}
}
}
return
true
return
true
...
...
test/integration/rewrites-client-rerender/next.config.js
deleted
100644 → 0
View file @
748c8e80
module
.
exports
=
{
rewrites
()
{
return
[
{
source
:
'/rewrite'
,
destination
:
'/?foo=bar'
,
},
]
},
}
test/integration/rewrites-client-rerender/pages/index.js
deleted
100644 → 0
View file @
748c8e80
import
{
useEffect
}
from
'react'
import
{
useRouter
}
from
'next/router'
export
default
function
Index
()
{
const
{
query
}
=
useRouter
()
useEffect
(()
=>
{
window
.
__renders
=
window
.
__renders
||
[]
window
.
__renders
.
push
(
query
.
foo
)
})
return
<
p
>
A
page
should
not
be
rerendered
if
unnecessary
.
<
/p
>
}
test/integration/rewrites-client-rerender/test/index.test.js
deleted
100644 → 0
View file @
748c8e80
/* eslint-env jest */
import
{
join
}
from
'path'
import
{
findPort
,
killApp
,
launchApp
,
nextBuild
,
nextStart
,
}
from
'next-test-utils'
import
webdriver
from
'next-webdriver'
const
appDir
=
join
(
__dirname
,
'../'
)
let
appPort
let
app
const
runTests
=
()
=>
{
it
(
'should not trigger unncessary rerenders'
,
async
()
=>
{
const
browser
=
await
webdriver
(
appPort
,
'/'
)
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
100
))
expect
(
await
browser
.
eval
(
'window.__renders'
)).
toEqual
([
undefined
])
})
it
(
'should rerender with the correct query parameter if present'
,
async
()
=>
{
const
browser
=
await
webdriver
(
appPort
,
'/rewrite'
)
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
100
))
expect
(
await
browser
.
eval
(
'window.__renders'
)).
toEqual
([
undefined
,
'bar'
])
})
}
describe
(
'rewrites client rerender'
,
()
=>
{
describe
(
'dev mode'
,
()
=>
{
beforeAll
(
async
()
=>
{
appPort
=
await
findPort
()
app
=
await
launchApp
(
appDir
,
appPort
)
})
afterAll
(()
=>
killApp
(
app
))
runTests
()
})
describe
(
'production mode'
,
()
=>
{
beforeAll
(
async
()
=>
{
await
nextBuild
(
appDir
)
appPort
=
await
findPort
()
app
=
await
nextStart
(
appDir
,
appPort
)
})
afterAll
(()
=>
killApp
(
app
))
runTests
()
})
})
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