Skip to content

Commit e3f6208

Browse files
committed
remove redux devtools from router reducer
1 parent c13f502 commit e3f6208

File tree

4 files changed

+39
-171
lines changed

4 files changed

+39
-171
lines changed

packages/next/src/client/components/app-router.tsx

+4-12
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ import {
3939
PathnameContext,
4040
PathParamsContext,
4141
} from '../../shared/lib/hooks-client-context.shared-runtime'
42-
import {
43-
useReducerWithReduxDevtools,
44-
useUnwrapState,
45-
type ReduxDevtoolsSyncFn,
46-
} from './use-reducer-with-devtools'
42+
import { useReducer, useUnwrapState } from './use-reducer'
4743
import { ErrorBoundary, type ErrorComponent } from './error-boundary'
4844
import { isBot } from '../../shared/lib/router/utils/is-bot'
4945
import { addBasePath } from '../add-base-path'
@@ -75,10 +71,8 @@ function isExternalURL(url: URL) {
7571

7672
function HistoryUpdater({
7773
appRouterState,
78-
sync,
7974
}: {
8075
appRouterState: AppRouterState
81-
sync: ReduxDevtoolsSyncFn
8276
}) {
8377
useInsertionEffect(() => {
8478
if (process.env.__NEXT_APP_NAV_FAIL_HANDLING) {
@@ -108,9 +102,7 @@ function HistoryUpdater({
108102
} else {
109103
window.history.replaceState(historyState, '', canonicalUrl)
110104
}
111-
112-
sync(appRouterState)
113-
}, [appRouterState, sync])
105+
}, [appRouterState])
114106
return null
115107
}
116108

@@ -219,7 +211,7 @@ function Router({
219211
actionQueue: AppRouterActionQueue
220212
assetPrefix: string
221213
}) {
222-
const [state, dispatch, sync] = useReducerWithReduxDevtools(actionQueue)
214+
const [state, dispatch] = useReducer(actionQueue)
223215
const { canonicalUrl } = useUnwrapState(state)
224216
// Add memoized pathname/query for useSearchParams and usePathname.
225217
const { searchParams, pathname } = useMemo(() => {
@@ -606,7 +598,7 @@ function Router({
606598

607599
return (
608600
<>
609-
<HistoryUpdater appRouterState={useUnwrapState(state)} sync={sync} />
601+
<HistoryUpdater appRouterState={useUnwrapState(state)} />
610602
<RuntimeStyles />
611603
<PathParamsContext.Provider value={pathParams}>
612604
<PathnameContext.Provider value={pathname}>

packages/next/src/client/components/use-reducer-with-devtools.ts

-153
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Dispatch } from 'react'
2+
import React, { use } from 'react'
3+
import { useCallback } from 'react'
4+
import type {
5+
AppRouterState,
6+
ReducerActions,
7+
ReducerState,
8+
} from './router-reducer/router-reducer-types'
9+
import type { AppRouterActionQueue } from '../../shared/lib/router/action-queue'
10+
import { isThenable } from '../../shared/lib/is-thenable'
11+
12+
export function useUnwrapState(state: ReducerState): AppRouterState {
13+
// reducer actions can be async, so sometimes we need to suspend until the state is resolved
14+
if (isThenable(state)) {
15+
const result = use(state)
16+
return result
17+
}
18+
19+
return state
20+
}
21+
22+
export function useReducer(
23+
actionQueue: AppRouterActionQueue
24+
): [ReducerState, Dispatch<ReducerActions>] {
25+
const [state, setState] = React.useState<ReducerState>(actionQueue.state)
26+
27+
const dispatch = useCallback(
28+
(action: ReducerActions) => {
29+
actionQueue.dispatch(action, setState)
30+
},
31+
[actionQueue]
32+
)
33+
34+
return [state, dispatch]
35+
}

packages/next/src/shared/lib/router/action-queue.ts

-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
ACTION_NAVIGATE,
88
ACTION_RESTORE,
99
} from '../../../client/components/router-reducer/router-reducer-types'
10-
import type { ReduxDevToolsInstance } from '../../../client/components/use-reducer-with-devtools'
1110
import { reducer } from '../../../client/components/router-reducer/router-reducer'
1211
import { startTransition } from 'react'
1312
import { isThenable } from '../is-thenable'
@@ -16,7 +15,6 @@ export type DispatchStatePromise = React.Dispatch<ReducerState>
1615

1716
export type AppRouterActionQueue = {
1817
state: AppRouterState
19-
devToolsInstance?: ReduxDevToolsInstance
2018
dispatch: (payload: ReducerActions, setState: DispatchStatePromise) => void
2119
action: (state: AppRouterState, action: ReducerActions) => ReducerState
2220
pending: ActionQueueNode | null
@@ -85,10 +83,6 @@ async function runAction({
8583

8684
actionQueue.state = nextState
8785

88-
if (actionQueue.devToolsInstance) {
89-
actionQueue.devToolsInstance.send(payload, nextState)
90-
}
91-
9286
runRemainingActions(actionQueue, setState)
9387
action.resolve(nextState)
9488
}

0 commit comments

Comments
 (0)