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

theme not loaded after page refresh in development mode #34

Closed
enterprisebug opened this issue Jan 5, 2018 · 13 comments
Closed

theme not loaded after page refresh in development mode #34

enterprisebug opened this issue Jan 5, 2018 · 13 comments

Comments

@enterprisebug
Copy link

Hi,

after selecting e.g. the LIGHT-THEME on your published website the theme gets restored when refreshing the page. That is what is expected...

When cloning your rep the theme gets not restored in development mode after refreshing the page with F5.

I tried debugging for quite a while but couldn't find any solution.
Do you have a fix for that?

Thanks,
Daniel

@tomastrajan
Copy link
Owner

Hmm, interesting. Can you see theme configuration in local storage for lolcalgost when in dev mode? I only remember how to check it in Chrome, the dev tools application tab.

@tomastrajan
Copy link
Owner

I did some testing and it seems to be working in Chrome but not in Firefox and IE. Will look into why later...

@enterprisebug
Copy link
Author

i have attached a screenshot for Chrome and IE11.

the setting gets stored but on initial load not restored.

even on settings page the stored theme gets not preselected.

i don't know if this is an issue of your repo but i am a fan of it because it shows a lot of great studd.

i tried reading: Initial State and Ahead of Time (AoT) Compilation but didn't get the clue.

2018-01-08 05_50_23-settings - angular ngrx material starter

@enterprisebug
Copy link
Author

okay. just observed on your PROD site:

It is working if ANMS-AUTH key is present.

application_key

If you delete this key then it is not working.
That should be an easy fix now. I will check it later.

@enterprisebug
Copy link
Author

@tomastrajan i thought it would be an easy fix. But actually i don't get it.
Do you have any other clue how to solve this?

@tomastrajan
Copy link
Owner

@enterprisebug yeah it should be related to the way we're initializing ngrx store on app startup, its looking for the keys per reducer / feature and probably this fails if the key (in our case ANMS-AUTH) is not present and probably the exception is swallowed silently. I am currently camping in Tasmania so not much internet or electricity is available but try checking / debugging local storage service in core module. Also, thank you very much for looking into this, I appretiate it a lot!

@tomastrajan
Copy link
Owner

So the problem is in the local storage service. This fails when the key is not found in local storage so we have to skip JSON parse in those cases.

@enterprisebug
Copy link
Author

I was not able to find any swallowed exceptions...

Loading the initial state seems to work in my opinion:

export function getInitialState() {
    const initialState = LocalStorageService.loadInitialState();
    console.log(initialState);
    return initialState;
}

this loads the setting:
2018-01-09 15_56_53-about - angular ngrx material starter

come back to a place where you have internet connection and electricity :-)

@enterprisebug
Copy link
Author

strange. even after commenting the selectorAuth

ngOnInit(): void {
  this.store
    .select(selectorSettings)
    .pipe(
      takeUntil(this.unsubscribe$),
      map(({ theme }) => theme.toLowerCase())
    )
    .subscribe(theme => {
      console.log(theme); // writes to console: default-theme
      this.componentCssClass = theme;
      this.overlayContainer.getContainerElement().classList.add(theme);
    });
  // this.store
  //   .select(selectorAuth)
  //   .pipe(takeUntil(this.unsubscribe$))
  //   .subscribe(auth => (this.isAuthenticated = auth.isAuthenticated));

@tomastrajan
Copy link
Owner

Can you see theme class applied correctly in HTML like <anms-root class="<theme-name>-theme"> ?

@enterprisebug
Copy link
Author

i am not authenticated.
i have deleted the ANMS-AUTH key from localstorage.
i have chose the BLACK-THEME.
2018-01-11 05_58_36-microsoft edge

this is loading the default-theme.
it produced the following HTML when loading the app:
2018-01-11 06_00_43-einstellungen

yes, it produces the correct HTML if the ANMS-AUTH key is present:
2018-01-11 06_03_48-aisto_xt debugging - microsoft visual studio

it it just not loading correct theme if the ANMS-AUTH is not present.

@tomastrajan
Copy link
Owner

Ok I found out what the problem was. The initial state was retrieved correctly. Only auth reducer is available during init phase (other 2 are lazy loaded) so when we reduce initial state without auth key auth reducer gets triggered and generates new state with its initial state. This new generated state is different from original one so it replaces it effectively throwing away other state keys for reducers which were not yet loaded.

Solution is to load and merge initial state on both INIT and UPDATE (reducers) actions. UPDATE action is triggered when lazy loaded reducer is added to the application.

tomastrajan added a commit that referenced this issue Jan 13, 2018
- prevent loss of initial state of lazy loaded reducers
- use meta reducer to merge local-storage state on both INIT and UPDATE (reducers) events
- add debug meta reducer in DEV mode
- run tests with PROD env to prevent debug reducer logging

- standardize payloads (always use payload object with typed properties)

Fixes #3, #34
@enterprisebug
Copy link
Author

@tomastrajan perfect! I'm really glad you found a solution!
Thanks man! I really appreciate it!

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

2 participants