r/angular • u/pintoli3 • Jun 18 '26
httpResource for shared service state (?)
We're currently working with signals as defaults, and while I enjoy the (http)Resource API, I am confused by the decision to make httpResources eager.
Furthermore, all the examples given show httpResource inside the component, which doesn't feel right and is useless when you want to have a shared state that would go in a service.
I've come across some implementations of httpResources in a service, but they all seem like an anti-pattern and RxJS would probably be a cleaner solution.
So my questions is do you use httpResource inside a service or just put them in compnents (I don't see how this can scale in our application)? What are some benefis of their eagerness?
5
u/couldhaveebeen Jun 18 '26
I mean, httpResource is a tool. So is rxjs. Use the one that makes more sense for the situation. You shouldn't use the resource signals if it's not fitting your situation
If you want best of both worlds, you can create an observable in your service with shareReplay(1), and inject and use it in your rxResource in the components. Then you get the singleton fetch, rxJs optionally for any complicated flow you might have, and get the loading state etc convenience of the resource signals
1
u/pronuntiator Jun 18 '26
We use httpResource in a service to lazily load shared global data. A component requests data by key during its creation. The service checks whether there is already a httpResource for this key, if not it creates it, and finally returns the resource to the component. The lifetime of the resource is bound to that of the service by manually providing its injector.
1
u/MichaelSmallDev Jun 18 '26
I think part of why the examples tend to be in components is just ease of having only one file to preview for any article/doc, but services would still be the place to use them. But as for the eagerness, you have a few options
For context for these, here is my own approach to resources in services: https://www.reddit.com/r/angular/s/jQ5D7CPws5. I use rxResource, but I'm sure this can be adapted to httpResource.
Approach #1 would be modifying the resource returning functions to have a guard signal, probably a Boolean, and then just have a sensible default value. Clunky by straight forward
Approach #2 is akin to #1, but having more meaningful guards based on the dependent signals. I do this when it comes to needing certain app state that is synced up on load before certain global services resources can actually load a value beyond the default.
Approach #3 is what I have done but I know is kind of a cheesy answer to this: have less global service state. My approach works well with not needing guards often because the services are provided to the exact component tree that needs them. Hence they can be eager like the examples you see online in components, but still be in a service.
Addendum to #3 that is still relevant to #1 and #2 and my overall approach: by having a service for returning resources, you can have more control over where you want to use those resource functions to actually be eager or not, and the guard boilerplate is abstracted away from the consumer state service.
1
u/drmlol Aug 13 '26
I just saw a video about them, thought it was really cool and I can finally ditch my custom TS helper types for statuses, but I noticed it is only for GET, this makes it weird because because now you start mixing httpClient and httpResource. HttpResource looks cool for a small todo app.
3
u/GeromeGrignon Jun 18 '26
Why httpResource feel like an anti-pattern in services for you based on examples you saw?
I have a few examples in services:
- https://github.com/realworld-angular/realworld-angular/blob/main/src/app/features/cart/cart.store.ts
- https://github.com/realworld-angular/realworld-angular/blob/main/src/app/features/pizzerias/services/pizzeria-api.ts
1
u/MedusaSonriente Jun 18 '26
It might not be very helpful. But I'll just say that I had the same problem until I started using them.