Code Splitting challenges with auto-generated action types #4698
-
I'm currently working through some challenges with large bundle sizes and the impact Redux code has on bundle size in our case is quite large. I'm familiar with the docs around code splitting but I have a more specific question around the recommended best practices of using auto-generated action types. When using Redux Toolkit, when you define an action creator, you get these nice action types that get auto generated for you which you can pass to a reducer: // FILE: /actions/counter.js
export const increment = createAction('counter/increment')
console.log(increment)
// => 'counter/increment' This makes it handy for passing the action directly to the // FILE: /reducers/counter.js
import {increment} from '../actions/counter';
const counterReducer = createReducer(0, (builder) => {
builder.addCase(increment, (state, action) => state + action.payload)
}) Note how Reducers need to get loaded on initial page load, but actions only need to be loaded when invoked by users. But since reducer files import action files, they import all the dependencies of those action files. So in the real world it might look more like this: // FILE: /actions/counter.js
// Note how we might be importing some large files here
import someGiantApiLibrary from '../apis/someGiantApiLibrary';
export const increment = createAsyncThunk(
'counter/increment',
async (name) => {
const response = await someGiantApiLibrary(`https://pokeapi.co/api/v2/pokemon/${name}`)
return await response.json()
}
); So then because The most obvious solution to this seems to be to NOT use the auto-generated action types and remove the action file import from the reducer, ie: // FILE: /reducers/counter.js
const counterReducer = createReducer(0, (builder) => {
// Just use the action type string here and no imports.
builder.addCase('counter/increment', (state, action) => state + action.payload)
}) But this is against the best practices defined here and also means the lose the type safety in the reducer. Which is a total bummer. Any advice on how to preserve the benefits of auto-generated action types without bloating the initial JS bundle? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
It's always tradeoffs. The action creators do generate predictable action names. Worst case, you decide that the bundle size aspect is too much of an issue, and hand-write the action type strings and manually annotate the TS types yourself. That said, I'm curious - just how big are the action-related files that you're pulling in? How much are they actually adding to the bundle size in practice? Also: how many of those async thunks could actually be replaced by use of RTK Query? |
Beta Was this translation helpful? Give feedback.
-
also reducers can be lazy loaded, which is what our combineSlices util is for |
Beta Was this translation helpful? Give feedback.
It's always tradeoffs.
The action creators do generate predictable action names. Worst case, you decide that the bundle size aspect is too much of an issue, and hand-write the action type strings and manually annotate the TS types yourself.
That said, I'm curious - just how big are the action-related files that you're pulling in? How much are they actually adding to the bundle size in practice?
Also: how many of those async thunks could actually be replaced by use of RTK Query?