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.src

On 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.

Note

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 |.

TriggerSelectorOptionsFires when
renderNoneNoneThe tag renders (cannot combine with |)
visible<selector>RequiredrootMargin=<css>The matched element enters the viewport
idleNonetimeout=<ms>The browser becomes idle
media<query>NoneNonematchMedia(query) matches
on<Event><sel>RequiredNoneThe 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).

Note

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.draft

visible

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.post

Pass 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" };

Recommendations

media

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)" };

MobileNav
Tip

Wrap 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"
EmojiPicker

Event 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
ChatWidget

Facade Tags

Because load applies per import site, wrapping a heavy tag in a facade makes it lazily loaded everywhere without repeating the trigger.

Warning

All lazy import sites of the same tag share one bundle, so multiple imports of the same tag should declare the same trigger. A facade keeps the trigger in one place.

Place the implementation inside a nested tags/ directory so only the facade can import it:

tags/
  location-map/
    index.marko
    tags/
      map-impl.marko
tags/location-map/index.marko
import MapImpl from "<map-impl>" with { load: "visible.location-map" };

<div.location-map>
  <MapImpl ...input/>
</div>
import MapImpl from "<map-impl>" with { load: "visible.location-map" };

div.location-map
  MapImpl ...input

Consumers use <location-map> directly with no load attribute, and the wrapper provides the selector target for visible.

Placeholders & 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.

Note

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.