Skip to content

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

Merged
alxhub merged 2 commits into
angular:mainfrom
JeanMeche:resource-state-key
May 6, 2026
Merged

alxhub merged 2 commits into
angular:mainfrom
JeanMeche:resource-state-key

Conversation

@JeanMeche

Copy link
Copy Markdown
Member

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

@pullapprove
pullapprove Bot requested review from AndrewKushnir and crisbeto April 15, 2026 21:32
@angular-robot angular-robot Bot added detected: feature PR contains a feature commit area: core Issues related to the framework runtime labels Apr 15, 2026
@ngbot ngbot Bot modified the milestone: Backlog Apr 15, 2026
Comment thread goldens/public-api/core/index.api.md Outdated
equal?: ValueEqualityFn<T>;
injector?: Injector;
params?: (ctx: ResourceParamsContext) => R;
transferCacheKey?: (params: R) => StateKey<T>;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copyied from a previous PR, @alan-agius4

  • 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.

Copy link
Copy Markdown
Member Author

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)?

And rxResource that calls HttpClient wouldn't need to use the cache here (thx to #67382). It they enabled the caching here, it would indeed duplicate the transfer.

I agree that we should provide a util to generate unit Ids.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am a bit confused as the this PR is linked to close issue #62897 which uses both rxResource with HttpClient, but you are saying this is already handled.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We considered that #67382 was broader than just HttpClient + rxResource.

Here we complete the caching feature by providing an option to cache anything (anything that is serializable).

@thePunderWoman thePunderWoman left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

reviewed-for: fw-general, public-api

@alan-agius4
alan-agius4 self-requested a review April 16, 2026 06:01
Comment thread packages/core/src/resource/resource.ts Resolved
Comment thread goldens/public-api/core/index.api.md Resolved
Comment thread packages/core/src/resource/resource.ts Resolved
Comment thread packages/core/src/resource/resource.ts Resolved
Comment thread packages/core/src/resource/api.ts Resolved
Comment thread packages/core/src/resource/resource.ts Resolved
@AndrewKushnir
AndrewKushnir removed their request for review April 17, 2026 22:10
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 3 times, most recently from dab8cc9 to 642433e Compare April 21, 2026 15:47
@JeanMeche
JeanMeche requested review from AndrewKushnir and removed request for crisbeto April 21, 2026 15:49
@JeanMeche
JeanMeche force-pushed the resource-state-key branch from 642433e to d485f4e Compare April 21, 2026 16:01
@JeanMeche JeanMeche modified the milestones: Backlog, v22 candidates Apr 23, 2026
@JeanMeche JeanMeche added action: merge The PR is ready for merge by the caretaker target: major This PR is targeted for the next major release labels May 5, 2026
@alxhub
alxhub merged commit 5a7c1e6 into angular:main May 6, 2026
22 of 23 checks passed
@alxhub

alxhub commented May 6, 2026

Copy link
Copy Markdown
Member

This PR was merged into the repository. The changes were merged into the following branches:

@angular-automatic-lock-bot

Copy link
Copy Markdown

This pull request has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jun 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime detected: feature PR contains a feature commit target: major This PR is targeted for the next major release

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

4 participants