Skip to content

Commit

Permalink
remove reducer from server bundle (#40959)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
feedthejim committed Sep 27, 2022
1 parent 6be2868 commit c69677a
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/next/client/components/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ type AppRouterState = {
/**
* Reducer that handles the app-router state updates.
*/
export function reducer(
function clientReducer(
state: Readonly<AppRouterState>,
action: Readonly<
| ReloadAction
Expand Down Expand Up @@ -1108,3 +1108,20 @@ export function reducer(
throw new Error('Unknown action')
}
}

function serverReducer(
state: Readonly<AppRouterState>,
_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

0 comments on commit c69677a

Please sign in to comment.