@defer
@defer holds back part of a template until a trigger fires, so the first screen renders sooner. On Lynx it delays creating views, not downloading code: the deferred component's code is already in your bundle.
Defer until a condition is true
src/app/feed.ts
- Before the trigger fires, Angular renders
@placeholder.<app-comments>isn't created, so none of its native elements exist yet. - When
showComments()becomestrue, Angular creates<app-comments>and removes the placeholder. - It stays created after that, even if the signal turns
falseagain. - Setting the signal in the tap handler is safe. Angular creates the deferred view after the handler returns.
Triggers
Load when an element scrolls into view
on viewport doesn't work, but the lynxExposure directive gives you the same signal. Put it on a wrapper and use when:
src/app/post.ts
area.visible()turnstruewhen the wrapper first appears on screen, for example when the user scrolls to it.- Give the wrapper a size. An element with no width or height never counts as visible.
The other blocks
@placeholdershows until the trigger fires.@placeholder (minimum 500ms)keeps it up for at least that long, to avoid a flash.@loadingshows while Angular loads the deferred code. Nothing downloads on Lynx, so this takes almost no time and@loadingshows only briefly, if at all. Addminimum, as in@loading (minimum 500ms), if you want it visible. Otherwise leave it out.@errorshows if loading fails. With the code already in the bundle, that's rare.
