From aef3617329a85f3c3a8d4a5c723ff3881e2a497d Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Tue, 27 Sep 2022 18:36:17 +0200 Subject: [PATCH] remove reducer from server bundle (#40959) This reducer takes a good chunk of the bundle but is never run on the server so we can eliminate it. 5-10ms wins from my manual benchmark runs. ![image](https://user-images.githubusercontent.com/11064311/192577550-4b2c3fa1-1ce9-456a-a635-d708e8200f2d.png) ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) --- packages/next/client/components/reducer.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/next/client/components/reducer.ts b/packages/next/client/components/reducer.ts index 6052c849e038e..0ab7615663923 100644 --- a/packages/next/client/components/reducer.ts +++ b/packages/next/client/components/reducer.ts @@ -615,7 +615,7 @@ type AppRouterState = { /** * Reducer that handles the app-router state updates. */ -export function reducer( +function clientReducer( state: Readonly, action: Readonly< | ReloadAction @@ -1108,3 +1108,20 @@ export function reducer( throw new Error('Unknown action') } } + +function serverReducer( + state: Readonly, + _action: Readonly< + | ReloadAction + | NavigateAction + | RestoreAction + | ServerPatchAction + | PrefetchAction + > +): AppRouterState { + return state +} + +// we don't run the client reducer on the server, so we use a noop function for better tree shaking +export const reducer = + typeof window === 'undefined' ? serverReducer : clientReducer