You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now 4.0 and Universal are out in the wild, we should see about making state hydration a bit simpler, and enabling developers to as automagically-as-possible transfer initial state from client -> server.
Reacted by Elwyn, Cory Rylan , Patrick Magee, Luca DeCaprio, Diego, antonyRoberts, Rakhat Rakhmetov, Mike Pearson, Fabien Dehopré, brandoncollins and 31 more
update - @vikerman advises me that we should have a built in for transferState in platform-server in the next couple of weeks, and we'll build off that 👍
Reacted by Sarun Intaralawan, Calebe Aires, Mike Pearson and Dmitri
I noticed that the snippets above use | json and JSON.parse. That will only handle plain JSON objects with the handful of primitive types, right?
Over in #143 and #160 it sounds like there is work underway that will make many Actions will have symbols in them, and will depend on the Action being of a specific class, not just an Object with the right data in it. Both of those things require a more complex serialization approach.
@kylecordes I don't think you'd be transferring actions this way, so non-serializable actions are probably fine (in this context). State is the thing being transferred here, which should almost always be serializable (the router state issue notwithstanding)...
@kylecordes I don't believe #143 or #160 will be going into ngrx. Classes are only used for actions to reduce the boilerplate of building type unions and action creators. Serialization is still a major focus for ngrx.
@MikeRyanDev That makes sense, but reducing boilerplate is not the only thing classes are used for. They are also used for type inference via the readonly type properties--I don't think we could have easily reproduced that behavior with plain objects and action creators, because you can't add a readonly property with an initialized value to an interface. Granted, that is only used compile-time. I had originally thought that ofClass fell into a similar category, but I guess the fact that it has run-time behavior makes it a bit more of an issue.
But I don't see class based actions, which we already use, interfering with state rehydration in universal, as long as the state itself remains serializable.
@karptonitereadonly is merely a side-effect of using classes. If you assign a string constant to the type property of a class it will widen the type to a string. The readonly modifier keeps the type narrow.
@Flood
I have a proposal here angular/universal#791 for the StateTransfer API along with draft implementations of each proposed API.
So it wouldn't take me long to actually implement it but I need other people on the team to sign off/review it.
If you want to help then voice your opinion over there.
@MattiJarvinen-BA's solution is cool. I just wonder, when you use effects, wouldn't you rather set and retrieve the transfer state in the effect where you make the service calls to the API?
@kaitlynekdahl Nice, thanks. Will try to implement that in one general effect running before the other effects to only be needed doing it once.
Where do you put transferState.remove? Wouldn't your current code always point to the transfer state version of your data when the state isn't removed after dispatched to the store?
please help with ngrx-router - I have router error with
this.transferState.onSerialize(NGRX_STATE, () => {
let state;
this.store.subscribe( ( saveState: any ) => {
// console.log('Set for browser', JSON.stringify(saveState));
state = saveState;
}).unsubscribe();
Hey @MattiJarvinen , want to know how should we handle the State that are forFeature for ex. StoreModule.forFeature('home', homeReducer) some part of root state was reset by "@ngrx/store/update-reducers"
@david-shortman As Universal is a first-class, Angular-supported feature/tool, I think it would be great to have first-class support in NgRx. However, since that's probably not going to be trivial to include in a generic sort of way that would work in everyone's setups, I think it should just be noted in the NgRx docs (cleaned up examples would be ideal, but just with a link to this thread would be super-helpful). Right now, the silence in the docs make it seems to me like 1) it should work out of the box or 2) that it's just fully unsupported.
Activity
MikeRyanDev commentedon Jul 19, 2017
Spitballing an idea of how we might tackle this:
robwormald commentedon Jul 23, 2017
update - @vikerman advises me that we should have a built in for transferState in platform-server in the next couple of weeks, and we'll build off that 👍
kylecordes commentedon Jul 23, 2017
I noticed that the snippets above use
| jsonandJSON.parse. That will only handle plain JSON objects with the handful of primitive types, right?Over in #143 and #160 it sounds like there is work underway that will make many Actions will have symbols in them, and will depend on the Action being of a specific class, not just an Object with the right data in it. Both of those things require a more complex serialization approach.
robwormald commentedon Jul 23, 2017
@kylecordes I don't think you'd be transferring actions this way, so non-serializable actions are probably fine (in this context). State is the thing being transferred here, which should almost always be serializable (the router state issue notwithstanding)...
MikeRyanDev commentedon Jul 23, 2017
@kylecordes I don't believe #143 or #160 will be going into ngrx. Classes are only used for actions to reduce the boilerplate of building type unions and action creators. Serialization is still a major focus for ngrx.
karptonite commentedon Jul 23, 2017
@MikeRyanDev That makes sense, but reducing boilerplate is not the only thing classes are used for. They are also used for type inference via the
readonlytype properties--I don't think we could have easily reproduced that behavior with plain objects and action creators, because you can't add areadonlyproperty with an initialized value to an interface. Granted, that is only used compile-time. I had originally thought thatofClassfell into a similar category, but I guess the fact that it has run-time behavior makes it a bit more of an issue.But I don't see class based actions, which we already use, interfering with state rehydration in universal, as long as the state itself remains serializable.
MikeRyanDev commentedon Jul 23, 2017
@karptonite
readonlyis merely a side-effect of using classes. If you assign a string constant to thetypeproperty of a class it will widen the type to a string. Thereadonlymodifier keeps the type narrow.Flood commentedon Sep 5, 2017
Any updates on this?
Toxicable commentedon Sep 5, 2017
@Flood
I have a proposal here angular/universal#791 for the StateTransfer API along with draft implementations of each proposed API.
So it wouldn't take me long to actually implement it but I need other people on the team to sign off/review it.
If you want to help then voice your opinion over there.
mcferren commentedon Sep 5, 2017
I also am very interesting in using this. stateTransfer object would replace initial state arg in the reducer function call?
RSginer commentedon Sep 7, 2017
any updates? im very interested to use this
Toxicable commentedon Sep 7, 2017
@RSginer it's currently in design
RSginer commentedon Sep 10, 2017
@Toxicable okey thanks, if i can help you tell me!
5 remaining items
MattiJarvinen commentedon Feb 22, 2018
@rijine everything related to transitioning server state to browser is in the code above. Basic angular server side render tutorials can be found on Angular.io https://angular.io/guide/universal
renestalder commentedon Mar 20, 2018
@MattiJarvinen-BA's solution is cool. I just wonder, when you use effects, wouldn't you rather set and retrieve the transfer state in the effect where you make the service calls to the API?
kaitlynekdahl commentedon Mar 22, 2018
@renestalder I'm doing something like this to first check TransferState for data, then calling the service if there's nothing there.
renestalder commentedon Apr 5, 2018
@kaitlynekdahl Nice, thanks. Will try to implement that in one general effect running before the other effects to only be needed doing it once.
Where do you put
transferState.remove? Wouldn't your current code always point to the transfer state version of your data when the state isn't removed after dispatched to the store?MattiJarvinen commentedon Apr 6, 2018
@renestalder that would be with
import { ROOT_EFFECTS_INIT } from '@ngrx/effects';Please do share your snippet when you got something to show.
newprogrammer1991 commentedon May 13, 2019
please help with ngrx-router - I have router error with
this.transferState.onSerialize(NGRX_STATE, () => {
let state;
this.store.subscribe( ( saveState: any ) => {
// console.log('Set for browser', JSON.stringify(saveState));
state = saveState;
}).unsubscribe();
mehrad-rafigh commentedon Aug 13, 2019
How would one solve this with the new creators syntax? Could not find any updated infos on that. Much appreciated
puneetv05 commentedon Jun 21, 2020
Hey @MattiJarvinen , want to know how should we handle the State that are
forFeaturefor ex.StoreModule.forFeature('home', homeReducer)some part of root state was reset by"@ngrx/store/update-reducers"david-shortman commentedon Feb 25, 2021
Based on the conversation, it seems NgRx is usable within Universal with some extra setup.
Would it be a goal of the project to give Universal first-class support, or is the demonstrated user-land capability to integrate sufficient?
matthewdickinson commentedon Jul 13, 2021
@david-shortman As Universal is a first-class, Angular-supported feature/tool, I think it would be great to have first-class support in NgRx. However, since that's probably not going to be trivial to include in a generic sort of way that would work in everyone's setups, I think it should just be noted in the NgRx docs (cleaned up examples would be ideal, but just with a link to this thread would be super-helpful). Right now, the silence in the docs make it seems to me like 1) it should work out of the box or 2) that it's just fully unsupported.
Thanks!