-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
code splitting strategy #4112
Comments
Hi! Just some general comments:
export const store = configureStore({
reducer: rootReducer,
- middleware: getDefaultMiddleware => getDefaultMiddleware().concat(listenerMiddleware.middleware),
+ middleware: getDefaultMiddleware => getDefaultMiddleware().prepend(listenerMiddleware.middleware),
})
import type { TypedStartListening, TypedAddListener } from "@reduxjs/toolkit"
import { createListenerMiddleware, addListener } from '@reduxjs/toolkit'
import type { RootState, AppDispatch } from './store'
export const listenerMiddleware = createListenerMiddleware()
export const startAppListening = listenerMiddleware.startListening as TypedStartListening<RootState, AppDispatch>
export const addAppListener = addListener as TypedAddListener<RootState, AppDispatch> then you can call these with effects that require the correct RootState and AppDispatch types. We don't have any examples of lazy loading slices with NextJS, but I think the same approach applies - call .inject in the slice file, and lazy load the component that imports the slice. I think your example repo is difficult to show the lazy loading because you're always injecting the slice - meaning you may as well have it as a static reducer. In comparison, see the example in #4064, where slices are only loaded once needed (once the tab is opened and an action is dispatched) |
@EskiMojo14 That's why I was thinking about whether it's possible to inject the required reducer on individual pages (although I'm not entirely sure if this approach is ok). It's somewhat similar to the example you provide, but in the example, Suspense is used for lazy importing the page component My goal is to reduce the first load JS shared by all page, as we don't want to load all reducers during the initial loading The result for the main branch is as follows: The above result can be found by using It seems like I've successfully reduced the size of In the // we can call both inject and injectInto, because the reducer reference is the same - injection only happens once regardless
const withCounterSlice = rootReducer.inject(counterSlice)
const injectedCounterSlice = counterSlice.injectInto(rootReducer) Does this mean that repeatedly injecting the same reducer won't have any impact, and it's safe to inject without any concerns? As for listenerMiddleware.startListening, is it safe to repeatedly start listening if the action and effect are the same? Thank you again for your help; I truly appreciate it. updated Approach 2 & Approach 3 has no effect on code splitting because CheckRequiredResource still references optionSlice |
Sorry, I think I've figured it out. Thank you again for your help |
sorry for the late response - yes, that looks right to me. |
@reduxjs/toolkit:
2.0.1
react-redux:
9.1.0
next:
14.1.0
Hi, recently I've been trying to use the new feature of createSlice in RTK V2 for code splitting, I encountered some uncertainties and errors. I reviewed the documentation but wasn't entirely sure, so I created this issue for clarification.
I created this repository to illustrate my questions, which include three branches:
main
,approach-2
, andapproach-3
main branch
In this branch, I injected the reducer on pages that require
optionalSlice
(index.tsx
,other.tsx
), and used listenerMiddleware to startListening for the required listenersQuestions
listenerMiddleware.startListening
for dynamically injecting listener effects, considering that the documentation indicates we should useaddListener
?index.tsx
,other.tsx
)? I am not sure if this is a proper approach. I am concerned it might cause some side effects. Although I tested it myself and it seems fine.startListening
, but I'm not sure where I went wrongapproach-2 branch
In this branch, not much has been changed. In a real-world app, adding logic to every required page might be impractical. Therefore, I wrote a
CheckRequiredResource.tsx
file, which wraps any component that meets the required conditions withInjectRoute
.Questions
Note
this approach has no effect on code splitting because CheckRequiredResource still references optionSlice.
you can ignore, I need to find another way
approach-3 branch
In this branch, I updated the
InjectRoute
. I used useEffect to inject the reducer and corresponding listener. Additionally, I added a stateisInitializeDone
to ensure that the children have the corresponding listener and reducer when rendering.It is evident that this approach has an impact on SEO.
Questions
addListener
, but I'm not sure where I went wrongNote
this approach has no effect on code splitting because CheckRequiredResource still references optionSlice.
you can ignore, I need to find another way
I'm not sure if it's appropriate to post it here. Thank you for your help.
The text was updated successfully, but these errors were encountered: