Skip to content

Commit

Permalink
Revert "Revert "Revert "Avoid unnecessary router state changes""" (#3…
Browse files Browse the repository at this point in the history
…7692)

Revert "Revert "Revert "Avoid unnecessary router state changes"" (#37593)"

This reverts commit 78cbfa0.
  • Loading branch information
ijjk authored Jun 14, 2022
1 parent 097574d commit a01aa4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 154 deletions.
99 changes: 28 additions & 71 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 @@ -1372,50 +1341,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)
}
} else {
// Still send the event to notify the inital load.
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)
}

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

0 comments on commit a01aa4d

Please sign in to comment.