Server rendering

Solid 2

Every piece on one wall, before the JavaScript

The archive's 600 pieces rise onto one wall from the first paint, in a wave, before the page's JavaScript has loaded; it takes over mid-wave.

Live demo

No JavaScript on this page: the wall is the server's HTML and CSS alone.

Reduced motion: The tiles fade in from the first paint, still in a wave, without growing or rising.

Why

A wall of hundreds of pieces is the first thing on the page, so its entrance should play from the first paint, not once the JavaScript has arrived. The wall is one createBatch with appear: true: the server writes its entrance into the page's head as one CSS animation for all 600 tiles, each tile's own start and target on its element as custom properties, and its stagger() its own delay. As the page hydrates, the Batch takes each tile over where its CSS has got to, at the speed it has there.

A CSS grid places the tiles, so the server needn't know how wide the page is: the Batch moves only what enters, their opacity, scale and rise. Load the page with the JavaScript held back two seconds, and the wave runs on the server's CSS alone, then carries on under the Batch without a jump; turn the JavaScript off, and every tile still rises to its place and stays.

Gains

  • The wall moves from the first paint, the JavaScript or none.
  • One CSS animation for 600 tiles, each with its own delay.

Costs

  • A few KB of CSS in the page's head, and about 85 bytes on each tile before compression.
  • Taking 600 animations over reads each once as the page hydrates: at thousands, give it to the ones on screen first.

The code

examples/wall/Demo.tsx
import { useLocation } from "@solidjs/router"
import { ARCHIVE } from "@wardic/example-storefront-shared/archive"
import { colourOf } from "@wardic/example-storefront-shared/glazes"
import { type Hydration, hydrationOf } from "@wardic/example-storefront-shared/hydration"
import { createBatch, stagger } from "@wardic/solid"
import { createSignal, For, onSettled, Show } from "solid-js"

/** When the JavaScript took over, in ms from the page's start: once, so a replay keeps it. */
let tookOver: number | undefined

/** What the server says is coming, before the JavaScript arrives. */
const waiting = (hydration: Hydration): string => {
  if (hydration.kind === "off") return "No JavaScript on this page: the wall is the server's HTML and CSS alone."
  if (hydration.kind === "late") return `Waiting for the JavaScript, held back ${hydration.ms / 1000} s…`
  return "Waiting for the JavaScript…"
}

/** Every piece in the archive on one wall, oldest first: one Batch, its entrance played from the first paint. */
export default function Wall() {
  const location = useLocation()
  const [at, setAt] = createSignal(tookOver)
  onSettled(() => {
    tookOver ??= Math.round(performance.now())
    setAt(tookOver)
  })

  // `appear`: the server writes the entrance into the page's CSS, so the tiles rise in from the first
  // paint; as the page hydrates, the Batch takes each one over where its CSS has got to. The grid
  // places them, so the server needn't know how wide the page is: the Batch moves only what enters.
  const tiles = createBatch({
    appear: true,
    count: ARCHIVE.length,
    initial: { opacity: 0, scale: 0.5, y: 12 },
    animate: () => ({ opacity: 1, scale: 1, y: 0 }),
    transition: { type: "spring", visualDuration: 0.5, bounce: 0.25, delay: stagger(0.004) },
  })

  return (
    <div class="wall-demo">
      <div class="wall" role="list" aria-label={`The archive's ${ARCHIVE.length} pieces, oldest first`}>
        <For each={ARCHIVE}>
          {(piece, i) => <span class="wall-tile" role="listitem" title={`${piece.name}, ${piece.year}`} {...tiles.at(i(), { style: { background: colourOf(piece.glaze) } })} />}
        </For>
      </div>
      <div class="hydration">
        <p class="hydration-status" role="status">
          <Show when={at()} fallback={waiting(hydrationOf(new URLSearchParams(location.search).get("hydrate")))}>
            {(ms) => <>The JavaScript took over at {ms()} ms; the wall had been rising since the first paint.</>}
          </Show>
        </p>
        {/* Full page loads, not the router's: `rel="external"`. */}
        <nav class="hydration-links" aria-label="Load this page again">
          <a class="button quiet" rel="external" href="/server/wall?hydrate=2000">
            Hydrate 2 s late
          </a>
          <a class="button quiet" rel="external" href="/server/wall?hydrate=off">
            Don't hydrate
          </a>
          <a class="button quiet" rel="external" href="/server/wall">
            Hydrate at once
          </a>
        </nav>
      </div>
    </div>
  )
}