Shipping less
Solid 2Rearrange the room: drag loads when you ask
The room's pieces stay put until you press Rearrange. Only then does the page fetch Wardic's drag, and the card says how long it took to arrive.
Reduced motion: Nothing changes: once drag has arrived, a piece follows your pointer, and nothing moves by itself.
Why
Most visitors look at a room plan and never move a thing, so the code that drags is weight they'd pay for nothing. This page gets domAnimation (hover, press, focus and in-view) from the shop's Wardic instance, createWardic(domAnimation), and nothing else. This room's pieces sit in a LazyWardic whose features is a function, () => import("~/lib/drag-feature.ts").then((m) => m.drag), and a lazy Feature loads as its section renders. The section renders when you press Rearrange, so that's when the page fetches drag. Until then the pieces are plain markup.
The cost is the wait. Switch on "Slow connection" and the import is held back 1.5 s: drag a piece meanwhile and nothing happens, and once drag arrives, the drag you tried isn't replayed. Props whose Feature hasn't arrived do nothing; values still animate at once. Replay the demo and drag arrives at once: the browser keeps a module it has loaded, as it would after a page that drags, such as Arrange a room.
A press is one trigger of several. Load on view, rendering the section as it scrolls in, for something below the fold that most visitors reach. Load after the first paint, rendering it once the page has mounted, for something nearly everyone uses that needn't hold up the first frame. Load on intent, as here, for something few use. And a page that uses a Feature from the start brings it with its route: every page in this shop that drags or lays out imports a Wardic instance of its own, createWardic([domAnimation, drag]), so the Feature is in the route's own chunk, there when the page is. This room uses a section instead: its load runs again at each press, so the card can time it and hold it back.
Gains
- A smaller first load: the page ships without
drag's code, about 7 KB gzipped here, fetched on the press. - The cost only on intent: a visitor who never rearranges never fetches it.
- The server's render is unchanged: the room is there, in place, before any script runs.
Costs
- Until
dragarrives, its props do nothing, and nothing is replayed: a drag begun mid-load is lost. - One more request, made just as the visitor wants to drag.
- A Feature to remember: each section names the Features it uses, and development warns of one left out.
In the docs: Shipping less
The code
import { LazyWardic, w } from "@wardic/solid"
import { createSignal, For, Show } from "solid-js"
const PIECES = [
{ id: "sofa", name: "Sofa", x: 24, y: 24, width: 120, height: 52 },
{ id: "table", name: "Table", x: 64, y: 112, width: 72, height: 44 },
{ id: "lamp", name: "Lamp", x: 236, y: 32, width: 44, height: 44 },
] as const
type Piece = (typeof PIECES)[number]
/** How long "Slow connection" holds the import back: long enough to try a drag before `drag` arrives. */
const HOLD_MS = 1500
const placeOf = (piece: Piece) => ({ left: `${piece.x}px`, top: `${piece.y}px`, width: `${piece.width}px`, height: `${piece.height}px` })
export default function Rearrange() {
const [wanted, setWanted] = createSignal(false)
const [slow, setSlow] = createSignal(false)
// How long `drag` took to arrive, from the press: absent until it has.
const [arrivedIn, setArrivedIn] = createSignal<number>()
let plan!: HTMLDivElement
let pressedAt = 0
let held = false
const rearrange = () => {
pressedAt = performance.now()
held = slow()
setWanted(true)
}
// The section's Features, loaded as it renders: it renders on the press, so that's when the page
// fetches `drag`. A page that has fetched it once, this one or another, has it at once.
const loadDrag = async () => {
if (held) await new Promise((resolve) => setTimeout(resolve, HOLD_MS))
const { drag } = await import("~/lib/drag-feature.ts")
setArrivedIn(Math.round(performance.now() - pressedAt))
return drag
}
const state = () => {
if (!wanted()) return "still"
if (arrivedIn() === undefined) return "loading"
return "arrived"
}
return (
<div class="room-demo">
<div class="lazy-bar">
<button class="button accent rearrange" disabled={wanted()} onClick={rearrange}>
Rearrange
</button>
<label class="switch">
<input type="checkbox" checked={slow()} disabled={wanted()} onChange={(event) => setSlow(event.currentTarget.checked)} />
<span>Slow connection</span>
</label>
<output class="lazy-status" aria-live="polite">
<Show when={wanted()} fallback={<><code>drag</code> isn't loaded: the pieces stay put.</>}>
<Show when={arrivedIn() !== undefined} fallback={<>Loading <code>drag</code>… a drag now does nothing.</>}>
<code>drag</code> arrived in {arrivedIn()} ms.
</Show>
</Show>
</output>
</div>
<div class="room-frame">
<div class="room-plan" ref={plan} data-drag={state()}>
<Show when={wanted()} fallback={<For each={PIECES}>{(piece) => <div class="room-piece" data-id={piece.id} style={placeOf(piece)}>{piece.name}</div>}</For>}>
{/* The section that brings `drag`, lazily: until it arrives, the pieces' drag props do nothing. */}
<LazyWardic features={loadDrag}>
<For each={PIECES}>
{(piece) => (
<w.div class="room-piece" data-id={piece.id} drag dragConstraints={() => plan} dragElastic={0.1} dragMomentum={false} whileDrag={{ scale: 1.05 }} style={placeOf(piece)}>
{piece.name}
</w.div>
)}
</For>
</LazyWardic>
</Show>
</div>
</div>
</div>
)
}