Skip to content

Commit

Permalink
Remove mutable field from action types
Browse files Browse the repository at this point in the history
Similar in spirit to #58938.

The app router reducer state used to be managed by useReducer, so it was
written to be resilient to rebasing — the same action may be processed
multiple times. Now that we've lifted the reducer outside of React
(#56497), each action runs only a single time. So we can simplify some
of the logic.

The purpose of the `mutable` field was so that if an action is
processed multiple times, state could be reused between each run; for
example, to prevent redundant network fetches. Now that this scenario
can no longer happen, we can remove it.

I had to update some of the unit tests in navigate-reducer because they
were written with the assumption that the reducer was called
multiple times. As far as I can tell, most of this behavior is covered
by e2e tests anyway, so I don't think it's too risky.
  • Loading branch information
acdlite committed Dec 4, 2023
1 parent 141dbb2 commit 969bf26
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 211 deletions.
5 changes: 0 additions & 5 deletions packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ function useServerActionDispatcher(dispatch: React.Dispatch<ReducerActions>) {
dispatch({
...actionPayload,
type: ACTION_SERVER_ACTION,
mutable: {},
})
})
},
Expand All @@ -189,7 +188,6 @@ function useChangeByServerResponse(
flightData,
previousTree,
overrideCanonicalUrl,
mutable: {},
})
})
},
Expand All @@ -209,7 +207,6 @@ function useNavigate(dispatch: React.Dispatch<ReducerActions>): RouterNavigate {
locationSearch: location.search,
shouldScroll: shouldScroll ?? true,
navigateType,
mutable: {},
})
},
[dispatch]
Expand Down Expand Up @@ -329,7 +326,6 @@ function Router({
startTransition(() => {
dispatch({
type: ACTION_REFRESH,
mutable: {},
origin: window.location.origin,
})
})
Expand All @@ -344,7 +340,6 @@ function Router({
startTransition(() => {
dispatch({
type: ACTION_FAST_REFRESH,
mutable: {},
origin: window.location.origin,
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ReadonlyReducerState,
ReducerState,
FastRefreshAction,
Mutable,
} from '../router-reducer-types'
import { handleExternalUrl } from './navigate-reducer'
import { handleMutable } from '../handle-mutable'
Expand All @@ -18,16 +19,10 @@ function fastRefreshReducerImpl(
state: ReadonlyReducerState,
action: FastRefreshAction
): ReducerState {
const { mutable, origin } = action
const { origin } = action
const mutable: Mutable = {}
const href = state.canonicalUrl

const isForCurrentTree =
JSON.stringify(mutable.previousTree) === JSON.stringify(state.tree)

if (isForCurrentTree) {
return handleMutable(state, mutable)
}

mutable.preserveCustomHistoryState = false

const cache: CacheNode = createEmptyCacheNode()
Expand Down
Loading

0 comments on commit 969bf26

Please sign in to comment.