Skip to content
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

Page reload writes default state to localStorage - Production ONLY #75

Closed
un33k opened this issue Dec 20, 2017 · 8 comments
Closed

Page reload writes default state to localStorage - Production ONLY #75

un33k opened this issue Dec 20, 2017 · 8 comments

Comments

@un33k
Copy link

un33k commented Dec 20, 2017

// in root reducer

export class Storage implements Action {
  readonly type = '@ngrx/store/storage';
  constructor(readonly payload: string) { }
}

export function localStorageSyncReducer(reducer: ActionReducer<State>): ActionReducer<State> {
  return (state: State, action: any) => {
    const keys = ['session'];

    if (action.type === '@ngrx/store/storage' && keys.includes(action.payload)) {
      const rehydratedState = rehydrateApplicationState([action.payload], localStorage, k => k, false);
      return { ...state, ...rehydratedState };
    }
    return localStorageSync({ keys, rehydrate: true, })(reducer)(state, action);
  }
}

export const metaReducers: MetaReducer<State>[] = !environment.production
  ? [localStorageSyncReducer, logger, storeFreeze]
  : [localStorageSyncReducer];
// in session service
  constructor(public store: Store<SessionState>) {
    window.addEventListener('storage', (event) => {
      this.store.dispatch(new Storage(event.key));
    }, false);
  }

The above works great in development, however, in production, an extra action of type @ngrx/store/update-reducers is fired twice which preempts the rehydrate step which wipes the localStorage copy to the initial state. Resulting in the user log-out when the page is reloaded.

"ngrx-store-localstorage": "^0.3.0",
@un33k
Copy link
Author

un33k commented Dec 20, 2017

As per this related issue, when StoreDevtoolsModule is enabled, everything works.

@un33k
Copy link
Author

un33k commented Dec 21, 2017

Update:

@ngrx/store/update-reducers comes after @ngrx/store/init.

Where @ngrx/store/init rehydrates the state from the localStorage.
Then @ngrx/store/update-reducers comes in which is not handled by this module. That ends up sending undefined in, as the state where the reducer returns the default.

To reproduce, just comment out this line from your module.
StoreDevtoolsModule.instrument(...).

@btroncone what do you think?

@envil
Copy link

envil commented Jan 9, 2018

I have the exactly same issue, please fix soon! Thank you!

@un33k
Copy link
Author

un33k commented Jan 11, 2018

Until this project becomes active again, here is a workaround ...

  1. Install the fix (by @nhaesler)
    -- yarn add https://github.com/un33k/ngrx-store-localstorage

  2. add this to your tsconfig.json

    "baseUrl": "src",
    "paths": {
      "@ngrx-storage-workaround/*": [
        "../node_modules/ngrx-store-localstorage/src/*"
      ]
  1. point to the new workaround package
import { localStorageSync, rehydrateApplicationState } from '@ngrx-storage-workaround/index';

This will pull down the typescript and compile it alongside your project.

@bufke
Copy link
Collaborator

bufke commented Jan 31, 2018

@btroncone I would be willing to help do maintenance work on this project if you want. I would not have time for reviewing feature work but I could review and merge regressions like this if you'd like. See my work here https://gitlab.com/bufke

@btroncone
Copy link
Owner

btroncone commented Feb 13, 2018

@bufke @un33k Apologies for the delay, I have been super busy (as I'm sure we all are) and lost track of this project. If anyone would like to take over and start managing issues or PR's I would be happy to make you a collaborator. I can oversee some PR's but I'm not sure how much time I will have to make active contributions in the near future.

Please let me know, and thank you for your patience! 👍 @ernestomancebo

@tanyagray
Copy link
Collaborator

Thanks @un33k and @bufke :) The PR by @nhaesler has been merged and publish as v5.0.0.

@SADIK112
Copy link

can anyone help me how to use the ngrx local storage module to encrypt and decrypt my data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants