@defer is the easiest performance win Angular has shipped in years, and it has one sharp edge that cost me an afternoon.
The win
Wrapping a heavy component in @defer (on viewport) moves it into its own chunk and loads it when it is needed. On one page this removed 180KB from the initial bundle in about ten minutes of work.
The edge
I deferred two grid components that between them occupied several thousand pixels. The placeholder was small, so the document was suddenly 12,823px instead of 6,833px — and every anchor link in the header scrolled to the wrong place, because the browser computed the target position against a document that had not been filled in yet.
- Give the placeholder the height the real content will occupy. This is the same discipline as reserving space for an image.
- Pick the trigger deliberately:
on viewportis right for something below the fold,on idlefor something that must exist before the user interacts. - Do not defer the LCP element. It seems obvious written down and it is easy to do by accident when the LCP element sits inside a larger block.



4 Comments
Leave a comment
Sign in to leave a comment.
Signing in is unavailable right now.







Peter Bakker
June 4, 2024The document height issue is one I would never have predicted. Did you find it through the anchor links or through CLS?
Muhammed Senussi
AuthorJune 4, 2024Through the anchor links, which is the worse way to find it because the symptom looks like a routing bug. CLS was fine, since nothing shifted — the page was simply the wrong length until the blocks loaded.
Yara Mansour
June 7, 2024هل تنصح باستخدام on idle أم on viewport كإعداد افتراضي؟
Muhammed Senussi
AuthorJune 7, 2024أستعمل أيّهما يأتي أولاً: on idle بعد التحميل أو on viewport. فالاعتماد على الرؤية وحدها يعني أن المحتوى لا يوجد أبداً لمن لا يمرّر الصفحة، وهذا يكسر البحث داخل الصفحة وروابط المرساة.