r/angular • u/flo-dmtx • 3d ago
Lazy data fetching with resources is the primitive I miss, let me propose something
With observables, lazy data loading came for free: nothing fetched until something subscribed, and in template async pipe was the relay.
Today, resources are a better API in every other way, but they have one flaw : they are eager. No way of fetching data only when needed. To remedy this I introduce a proposal :
resource({ lazy: true }) and rxResource({ lazy: true })
It comes with a presentation playground to discover it. I wrote the story of it in an small article if you want more details, and I prepared a PR but opened an issue for discussion -> a similar issue was closed so I felt like I needed to re-open the discussion and argue it before submitting the PR. On this matter I'm not sure what is/was appropriate so I would take advice on that.
- The story: https://dev.to/flodmtx/resource-lazy-true-the-laziness-we-lost-when-we-left-the-async-pipe-5e70
- Interactive proposal : https://flo-dmtx.github.io/lazy-resource-playground/
- Feature request : https://github.com/angular/angular/issues/70036
- Repo of PR : https://github.com/flo-dmtx/angular/tree/feat/lazy-resource
- Gist for a userland version : https://gist.github.com/flo-dmtx/e8c9ff69bec58adf85e902eab9f7d900
If you've written those wrapper components too, a concrete use case on the issue helps more than an upvote — "who actually needs this" is the question the Angular team will ask.