Question presented to candidate:
"On our product list, users type a search, scroll, open a product, and press Back. The list rebuilds from scratch and their search is gone. How would you keep that list alive across navigations with a custom RouteReuseStrategy, and what are the risks?"
What a strong answer should cover:
RouteReuseStrategyhas five hooks:shouldDetach/storewhen leaving a route,shouldAttach/retrievewhen entering one, andshouldReuseRoutefor staying on the same config.- The default (
BaseRouteReuseStrategy) reuses the live instance only when the route config is the same (for example/products/7to/products/8) and never stores detached routes. - A custom strategy, usually extending
BaseRouteReuseStrategyand provided with{ provide: RouteReuseStrategy, useClass: ... }, stores aDetachedRouteHandlefor chosen routes so the same component instance, DOM and state come back. - A detached component is not destroyed and gets no lifecycle hook on return; use the outlet's
(attach)/(detach)events. - Risks: memory (stored trees live until evicted,
destroyDetachedRouteHandlein 22.2), stale data, and cache keys that collide or never match.
Clarifying questions expected:
- "Should the list also keep data fresh when the user returns, or show exactly what they left?"
- "How many screens might be kept alive at once?"
Code / implementation expected: Yes β a keep-alive strategy for routes flagged in data, with constructor and destroy counts across navigations.