Performance: Resolver for pre-fetching data
MEDIUM IMPACT
This affects the page load speed by delaying route activation until data is ready, improving perceived load but potentially increasing initial wait time.
export const dataResolver = () => { const dataService = inject(DataService); const route = inject(ActivatedRouteSnapshot); return dataService.getData(route.params['id']); }; const routes = [ { path: 'item/:id', component: ItemComponent, resolve: { itemData: dataResolver } } ];
this.route.params.subscribe(params => { this.dataService.getData(params['id']).subscribe(data => { this.data = data; }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Data fetched inside component after route loads | Multiple DOM updates as data arrives | Multiple reflows triggered | Multiple paints increasing load time | [X] Bad |
| Data fetched in resolver before route activation | Single DOM update with data ready | Single reflow on initial render | Single paint with stable layout | [OK] Good |