Skip to content

Resource state key#67401

Draft
JeanMeche wants to merge 2 commits intoangular:mainfrom
JeanMeche:resource-state-key
Draft

Resource state key#67401
JeanMeche wants to merge 2 commits intoangular:mainfrom
JeanMeche:resource-state-key

Conversation

@JeanMeche
Copy link
Member

On top of #67382

feat(core): add ability to cache resources for SSR

This commit adds a transferCacheKey option to enable easy caching for resource/ rxResource.

By caching resource data we make sure that resources are not in a loading state during hydration on the client side and responsible for destroying server hydrated DOM.

fixes #62897

In order for resources to allow caching in SSR context (eg in the TransferState), resource need to be able to set their value synchronously.

If the resource value is not set synchronously, the resource will be in in a "loading" state which is responsible for destroying the server-hydrated resolved DOM.
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: core Issues related to the framework runtime labels Mar 3, 2026
@JeanMeche JeanMeche requested a review from alxhub March 3, 2026 00:32
@ngbot ngbot bot added this to the Backlog milestone Mar 3, 2026
@JeanMeche JeanMeche force-pushed the resource-state-key branch from cd92c68 to 39f6641 Compare March 3, 2026 00:34
@thePunderWoman
Copy link
Contributor

D39YrGcXsAA3K98

This commit adds a `transferCacheKey` option to enable easy caching for `resource`/ `rxResource`.

By caching resource data we make sure that resources are not in a loading state during hydration on the client side and responsible for destroying server hydrated DOM.

fixes angular#62897
@JeanMeche JeanMeche force-pushed the resource-state-key branch from 39f6641 to 6af5943 Compare March 3, 2026 00:54
equal?: ValueEqualityFn<T>;
injector?: Injector;
params?: (ctx: ResourceParamsContext) => R;
transferCacheKey?: (params: R) => StateKey<T>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Does this implementation result in duplicate transfer state entries (HTTP Client andResource)?
  • Consider id maybe instead of transferCacheKey?
  • Manual key management places a heavy burden on the developer to ensure global uniqueness. To improve the DX, we should explore internal ID generation instead of it being part of the API or something similar to React’s useId or Vue’s composition helpers. Relying on users to manual create unique cache-key feels like a "foot-gun" especially in big organizations; we should ideally handle key construction internally to prevent collisions, this was one of the reasons that we always avoided providing such option in the HTTP transfer cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime detected: feature PR contains a feature commit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resource hasValue() is false for a moment when using TransferState cached data, causing hydration to fail

3 participants