Question presented to candidate: "A product page shows an empty template with spinners for half a second, then jumps when data arrives. A colleague suggests a route resolver. What is a resolver, when does it actually help, and what does it cost?"
What a strong answer should cover:
- A resolver (
ResolveFn<T>) is a function listed under a route'sresolvekey; the router runs it after all guards pass and before the component is created, and stores the result in the route'sdata. - The component receives the value through
ActivatedRoute.dataor, withwithComponentInputBinding(), as an input named after the key. - Cost: the navigation (and the old screen) waits for the slowest resolver, so slow APIs make the app feel frozen; show progress via router events.
- Behaviour details: Promise or Observable allowed; the router uses the first emitted value; an Observable that completes without emitting cancels the navigation; an error becomes a
NavigationError; returning aRedirectCommandredirects. - Re-run rules: by default resolvers re-run when path or matrix parameters change, not on query changes (
runGuardsAndResolvers). - When not to use one: data that can stream in, or that should not block the URL change.
Clarifying questions expected:
- "How slow is the API, and is a blank page or a frozen old page worse for these users?"
- "What should happen when the product does not exist: a 404 page or an inline message?"
Code / implementation expected: Yes β a functional resolver that fetches by route id, redirects when not found, and binds to a component input.