One file of code the browser downloads on its own.In the glossary
of click code downloads with the page, and runs only when its own click happens.
import { state } from '@markless/core';export default functionGalleryfunction Gallery(): Element() @{@{ · Statement containerHolds instructions: setup lines, then the element to show. letopenlet open: string=statestate · Watched valueDeclares a variable the page watches. You read and assign it like any other variable, and the page keeps up.(''); <button<button> · HTML elementA button a person can press.onClickonClick · Event handlerRuns when this element gets the DOM event named after on, with the browser's own event object.={() => (open = 'Bay')}>Bay</button<button> · HTML elementA button a person can press.>}
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:
Hover a photo or Share, then press it.
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@if · ConditionalShows its block while the test is true. 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.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import { element, state } from '@markless/core';export default function Riddle() @{ const field = element<HTMLInputElement>(); let asked = state(false); function answer() { field?.focus();asked = true; } <sectionclass={asked ? 'riddle nasty' : 'riddle'} > <pclass="face" /> <q>What has keys but opens no locks?</q> <inputel={field} placeholder="Your answer" /> <buttononClick={answer}> {asked ? 'Answering…' : 'Answer him'} </button> </section>}
To do: Press Answer him. The cursor lands in the box through the field handle, and Smeagle turns to his nasty side.
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.