Lazy Loading
By default, every custom tag used on a page is bundled into that page's JavaScript. Adding a load import attribute splits the tag into its own browser bundle, fetched only after a trigger fires.
import VideoPlayer from "<video-player>" with { load: "visible#hero" };
<section#hero>
<VideoPlayer src=input.src/>
</section>import VideoPlayer from "<video-player>" with { load: "visible#hero" };
section#hero
VideoPlayer src=input.srcOn the server, a lazily loaded tag renders its HTML immediately like any other tag; only its browser JavaScript is deferred until the trigger fires and the bundle arrives. In the browser, nothing renders in the tag's place until both the trigger and bundle resolve, and <try> can display placeholder content in the meantime.
Attributes passed to a lazily loaded tag stay reactive while it loads, so the tag receives the latest values when its bundle arrives.
The load attribute applies to default imports of custom tags (relative paths or tag import shorthand) declared before their first use. Lazily loaded tags render directly without tag variables.
Triggers
The load value is a static string read at build time: either "render" alone, or one or more triggers joined with |.
| Trigger | Selector | Options | Fires when |
|---|---|---|---|
render | None | None | The tag renders (cannot combine with |) |
visible<selector> | Required | rootMargin=<css> | The matched element enters the viewport |
idle | None | timeout=<ms> | The browser becomes idle |
media<query> | None | None | matchMedia(query) matches |
on<Event><sel> | Required | None | The event first fires on the matched element |
Selectors match via document.querySelector: visible#hero, visible.hero, and visible[data-hero] attach directly, while a bare type selector needs a leading space (visible section).
During SSR, the trigger script runs as the tag's HTML chunk flushes, so the watched element must already exist in the document. If a selector matches no element, the bundle loads immediately.
render
Splits the tag into its own bundle loaded as soon as the tag renders, useful for conditional or heavy components. Must be used alone (cannot combine with multiple triggers).
import MarkdownEditor from "<markdown-editor>" with { load: "render" };
<MarkdownEditor value=input.draft/>import MarkdownEditor from "<markdown-editor>" with { load: "render" };
MarkdownEditor value=input.draftvisible
Loads the bundle when the matched element enters the viewport via IntersectionObserver:
import Comments from "<comments>" with { load: "visible.comments" };
<section.comments>
<Comments post=input.post/>
</section>import Comments from "<comments>" with { load: "visible.comments" };
section.comments
Comments post=input.postPass rootMargin in a query string to preload before the element enters the viewport:
import Comments from "<comments>" with { load: "visible.comments?rootMargin=100px" };import Comments from "<comments>" with { load: "visible.comments?rootMargin=100px" };idle
Loads the bundle when the browser is idle via requestIdleCallback (or immediately if unsupported). Accepts an optional timeout in milliseconds and no selector:
import Recommendations from "<recommendations>" with { load: "idle?timeout=2000" };
<Recommendations/>import Recommendations from "<recommendations>" with { load: "idle?timeout=2000" };
Recommendationsmedia
Loads the bundle once matchMedia matches the given media query (immediately if already matched). Accepts both parenthesized media features (media(max-width: 768px)) and media types (media print):
import MobileNav from "<mobile-nav>" with { load: "media(max-width: 768px)" };
<MobileNav/>import MobileNav from "<mobile-nav>" with { load: "media(max-width: 768px)" };
MobileNavWrap a media-triggered tag in a facade to apply the trigger consistently at every call site.
Events
A trigger starting with on followed by - or a capital letter loads the bundle the first time that event fires on the matched element:
import EmojiPicker from "<emoji-picker>" with { load: "on-focus[name=message]" };
<input name="message" placeholder="Say something nice">
<EmojiPicker/>import EmojiPicker from "<emoji-picker>" with { load: "on-focus[name=message]" };
input name="message" placeholder="Say something nice"
EmojiPickerEvent casing follows native event handler rules: on- preserves casing, while onCamelCase lowercases the event name (on-click and onClick are equivalent).
Multiple Triggers
Join triggers with | to load as soon as the first one fires:
import ChatWidget from "<chat-widget>" with { load: "on-mouseover#chat | idle?timeout=5000" };
<button#chat>Chat with us</button>
<ChatWidget/>import ChatWidget from "<chat-widget>" with { load: "on-mouseover#chat | idle?timeout=5000" };
button#chat -- Chat with us
ChatWidgetPlaceholders & Errors
In the browser, a lazily loaded tag integrates with <try> like <await>: @placeholder renders while waiting for the trigger and while the bundle fetches, and @catch handles load failures:
import PriceChart from "<price-chart>" with { load: "visible.chart" };
<figure.chart>
<try>
<PriceChart symbol=input.symbol/>
<@placeholder>Loading chart...</@placeholder>
<@catch|err|>Failed to load chart: ${err.message}</@catch>
</try>
</figure>import PriceChart from "<price-chart>" with { load: "visible.chart" };
figure.chart
try
PriceChart symbol=input.symbol
@placeholder -- Loading chart...
@catch|err| -- Failed to load chart: ${err.message}Bundler Support
Lazy loading relies on the linkAssets compiler option, which @marko/vite and Marko Run configure automatically.
Builds without linkAssets (such as @marko/vite with linked: false) cannot code-split; with { load } imports compile as eager imports and render normally.
Contributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.