Skip to content

code splitting strategy #4112

@aNyMoRe0505

Description

@aNyMoRe0505

@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, and approach-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 listeners

rootReducer.inject(optionalSlice);
listenerMiddleware.startListening({ actionCreator: optionalIncrement, effect: onOptionalIncrementEffect });
listenerMiddleware.startListening({ actionCreator: optionalDecrement, effect: onOptionalDecrementEffect });

Questions

  1. Is it okay to use listenerMiddleware.startListening for dynamically injecting listener effects, considering that the documentation indicates we should use addListener?
  2. Is it okay to write the same logic in two page components (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.
  3. I encountered some TypeScript issues when using startListening, but I'm not sure where I went wrong
截圖 2024-01-23 16 01 29

approach-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 with InjectRoute.

Questions

  1. Just want to check if this approach is okay or not

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 state isInitializeDone to ensure that the children have the corresponding listener and reducer when rendering.
It is evident that this approach has an impact on SEO.

  useEffect(() => {
    const unsubscribeFn: any[] = [];

    // some condition needed
    rootReducer.inject(optionalSlice);

    unsubscribeFn.push(dispatch(addListener({ actionCreator: optionalIncrement, effect: onOptionalIncrementEffect  })))
    unsubscribeFn.push(dispatch(addListener({ actionCreator: optionalDecrement, effect: onOptionalDecrementEffect  })))

    setIsInitializeDone(true);

    return () => {
      unsubscribeFn.forEach(fn => fn());
    };
  }, []);

Questions

  1. I am uncertain whether it is ok to repeatedly inject the same reducer and listener. If it is allowed, does that mean there is no need to unsubscribe listener and inject the reducer based on conditions (like state is undefined or not)?
  2. I encountered some TypeScript issues when using addListener, but I'm not sure where I went wrong
截圖 2024-01-23 15 32 36

Note

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions