question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug with Lazy Loaded feature modules

See original GitHub issue

Hello,

The following PR introduced a bug related to lazy loaded feature modules: https://github.com/btroncone/ngrx-store-localstorage/pull/76/files

The net effect of this PR is to re-hydrate the state when every feature module is loaded, and this breaks sections of the state which are selectively serialized (localStorageSync({keys: [{todos: ['name', 'status'] }, ... ]}))).

The result is the state being rehydrated and deleting all other keys not in local storage.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:15
  • Comments:37 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
btronconecommented, Jun 7, 2018

@bmalinconico Really looking for another extra set of eyes that is using this library every day. Need help determining priority and impact of some of the proposed PR’s / issues.

0reactions
ivayloccommented, Oct 13, 2022

I am using ngrx-store-localstorage with NX workspace and I tried all workarounds but still can’t make it work with lazyloaded modules in the case when I want to rehydrate specific slices from the state, on page refresh the initial state is being replaced by the saved slices from the localstorage.

The root store is called merchant and the feature store is called onboarding.

This is my setup:

merchant.meta-reducers.ts - for the host app

const INIT_ACTION = '@ngrx/store/init';
const UPDATE_ACTION = '@ngrx/store/update-reducers';

const mergeReducer = (state: any, rehydratedState: any, action: any) => {
  if (
    (action.type === INIT_ACTION || action.type === UPDATE_ACTION) &&
    rehydratedState
  ) {
    state = merge(state, rehydratedState);
  }
  return state;
};

export function sessionStorageSyncReducer(
  reducer: ActionReducer<any>
): ActionReducer<any> {
  return localStorageSync({
    keys: [
      {
        merchant: [
          { authProcess: ['id'] },
          { stepTasks: [{ 0: ['id'] }] },
          { registrationDetails: ['email'] },
        ],
      },
      {
        onboarding: [
          { onboardingProcess: ['id'] },
          { stepTasks: [{ 0: ['id'] }] },
        ],
      },
    ],
    rehydrate: true,
    removeOnUndefined: true,
    storage: sessionStorage,
    mergeReducer,
  })(reducer);

app.module.ts - host app

    StoreModule.forRoot(
      { merchant: merchantsReducer },
      {
        metaReducers: [sessionStorageSyncReducer],
        runtimeChecks: {
          strictActionImmutability: true,
          strictStateImmutability: true,
        },
      }
    ),

entry.module.ts

    StoreModule.forFeature(
      'onboarding',
      onboardingReducer,
      {
        metaReducers: [sessionStorageSyncReducer],
        initialState,
      }
    ),

onboarding.meta-reducer.ts - metareducer for the lazyloaded module

export function sessionStorageSyncReducer(
  reducer: ActionReducer<any>
): ActionReducer<any> {
  return localStorageSync({
    keys: [
      {
        onboarding: [
          { onboardingProcess: ['id'] },
          { stepTasks: [{ 0: ['id'] }] },
        ],
      },
    ],
    rehydrate: true,
    storage: sessionStorage,
  })(reducer);
}

export const metaReducers: Array<MetaReducer<any, any>> = [
  sessionStorageSyncReducer,
];
Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolving errors faced while implementing Lazy Loading with ...
It is a known bug in Angular that lazy loading module does not work well with Auxiliary routes / named router.
Read more >
Issues for Lazy-load | Drupal.org
Title Status Priority Category Version Com... iframes no longer lazy load after 3.6 Active Normal Bug report 8.x‑3.x‑dev Code Automated Drupal 10 compatibility fixes Needs...
Read more >
netmonitor can lazy load various components modules
Here is a profile of DAMP: https://perfht.ml/2zwbLsH It highlights that MonitorPanel is force-loaded by App.js and it pulls many dependencies ...
Read more >
Angular Example - Lazy Loading Feature Modules - StackBlitz
Angular Example - Lazy Loading Feature Modules. ... customers.module').then(m => m. CustomersModule). },. {. path: 'orders',. loadChildren: () => import('.
Read more >
Angular Feature Module And Lazy Loading - One Of Way To ...
It will avoid the loading of the entire application at a time. · Can be pre-loaded the features modules in the background ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found