Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Avoid unnecessary router state changes" #37572

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 28 additions & 68 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -1343,47 +1312,38 @@ export default class Router implements BaseRouter {
const shouldScroll = options.scroll ?? !isValidShallowRoute
const resetScroll = shouldScroll ? { x: 0, y: 0 } : null

const nextScroll = forcedScroll ?? resetScroll
const mergedNextState = {
...nextState,
route,
pathname,
query,
asPath: cleanedAs,
isFallback: false,
}

// for query updates we can skip it if the state is unchanged and we don't
// need to scroll
// https://github.com/vercel/next.js/issues/37139
const canSkipUpdating =
(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
})
await this.set(
{
...nextState,
route,
pathname,
query,
asPath: cleanedAs,
isFallback: false,
},
routeInfo,
forcedScroll ?? resetScroll
).catch((e) => {
if (e.cancelled) error = error || e
else throw e
})

if (error) {
Router.events.emit('routeChangeError', error, cleanedAs, routeProps)
throw error
}
if (error) {
Router.events.emit('routeChangeError', error, cleanedAs, routeProps)
throw error
}

if (process.env.__NEXT_I18N_SUPPORT) {
if (nextState.locale) {
document.documentElement.lang = nextState.locale
}
if (process.env.__NEXT_I18N_SUPPORT) {
if (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
const hashRegex = /#.+$/
if (shouldScroll && hashRegex.test(as)) {
this.scrollToHash(as)
}
// A hash mark # is the optional last part of a URL
const hashRegex = /#.+$/
if (shouldScroll && hashRegex.test(as)) {
this.scrollToHash(as)
}

return true
Expand Down
10 changes: 0 additions & 10 deletions test/integration/rewrites-client-rerender/next.config.js

This file was deleted.

13 changes: 0 additions & 13 deletions test/integration/rewrites-client-rerender/pages/index.js

This file was deleted.

55 changes: 0 additions & 55 deletions test/integration/rewrites-client-rerender/test/index.test.js

This file was deleted.