NG.II · Angular 22 practice question
A developer is implementing a data-fetching mechanism using Angular's `resource` function and needs to ensure the initial server-rendered HTML correctly reflects the loaded data during subsequent client-side hydration. What is the primary requirement for the `id` property when configuring this resource?
const userResource = resource({
params: () => ({id: userId()}),
loader: ({params}) => fetchUser(params),
id: 'user-unique-id',
});
Check answer and explanation
Correct answer: The provided `id` must be unique across the entire application and must match exactly between the server and client execution contexts.
Explanation
The specification mandates that the identifier must possess uniqueness throughout the application and maintain exact consistency between the server and client execution environments to allow Angular to correctly associate the cached entry with the requesting resource.
Common trap
Assigning the identifier to be dependent on the current user session undermines the objective of caching the server's output, and excluding the identifier prevents the state transfer mechanism from functioning.
Source: Angular docs: Async reactivity with resources — Caching `resource` data with SSR
Checked against the cited text on 2026-11-25.