Lazy chunks

3 min·Assumes: state, events, elements

Each
of click code downloads with the page, and runs only when its own click happens.
import { state } from '@markless/core';

export default function Gallery() @{
	let open = state('');

	<button onClick={() => (open = 'Bay')}>Bay</button>
}

onClick becomes a small function of its own. It downloads while the page loads, and runs only when someone presses Bay.

Downloading is not running

Open a photo or press Share, and watch the network panel:

A pen-and-ink drawing of a photo gallery in a browser window, with three photos and a Share button, beside a network panel. The panel lists photos.html, open-photo.js and share.js: all three arrived when the page loaded, and only the page has run. Press a photo, or pick one with the arrow keys and press Enter, to open it large: open-photo.js is marked as run on that click, and nothing new downloads. Press Share, or S, and share.js runs the same way. Escape closes the photo and starts again.

A photo gallery. Its click code, open-photo.js and share.js, was downloaded with the page and has not run.

  • Both click files arrived with the page.
  • Each one runs only on its own click.
  • Nothing new downloads when you press.

What a small page pays

Measured in Chromium for a one-page @markless/router app with one button and one line of text (uncompressed, main at 2efb1c6e2):

  • Executed at load: 8.4 kB, four small inline scripts and the router's link script.
  • Downloaded at load, not executed: 23.6 kB, fetched with modulepreload at low priority.
  • Executed on the first click: that same 23.6 kB, with nothing new to download.

Every handler and update is a symbol

The compiler cuts a component into symbols: one small function each for an event handler, a text or attribute update, an attach={...} behavior, a computed() recipe, a state() starting value and an @if or async update. A symbol runs only when its trigger fires, and no handler runs at mount.

Packing decides which file holds which code

Production client builds put the symbols into a few files, cut by what each page needs and when:

  • First use

    What the page load and its first interactions run. It downloads with the page.

  • Navigation

    What only a client-side navigation runs. It downloads when a navigation is about to happen.

  • Deferred

    Everything else. It downloads on its first import().

Each module in a pack still starts only when something first imports it. Set packing: false in the markless() options to ship one file per module.

A symbol reaches only what crosses

A handler runs later than the component body, so it can only use what Markless can find again, like state and element handles.

PlaygroundRestored your edit

What has keys but opens no locks?
Try this
  1. To do: Press Answer him. The cursor lands in the box through the field handle, and Smeagle turns to his nasty side.
  2. To do: Try breaking it: above the elements, add const box = document.querySelector('input'); and change field?.focus() to box?.focus(). A page element found that way can't cross into the handler.

Native targets asks whether a browser has to run these symbols at all.