NOSCRIPT

Page 05: The router

Retires history.pushState() and the single-page app built to animate a link

For about ten years, the price of an animated page change was to stop having pages.

A browser moving from one document to the next threw the old one away before it painted the new one. If you wanted the two to overlap, even for a third of a second, both had to live in the same document. So we intercepted every link, fetched the next page as data and rebuilt it in place. Then we rebuilt by hand what the browser had been doing for free: the address bar, the back button, the scroll position, focus, and the announcement to a screen reader that a new page had arrived.

Cross-document view transitions remove the reason. When you follow a link to the same origin, the browser keeps a picture of the page you are leaving, loads the next one, and animates between the two. Both pages opt in with one rule.

Wasa router, abridged
addEventListener("click", async e => {
  const a = e.target.closest("a");
  if (!a || a.origin !== location.origin)
    return;
  e.preventDefault();
  const res = await fetch(a.href);
  history.pushState({}, "", a.href);
  swap(parse(await res.text()));
  restoreScroll(); moveFocus(); announce();
});
Nowthe whole router
@view-transition {
  navigation: auto;
}

What you have been watching

This zine is six separate HTML files. If you are reading it in Chrome 126 or later, or Safari 18.2 or later, and have not asked your system to reduce motion, three things have happened each time you followed a link. I have to write “if” because CSS has no test for this feature, so the page cannot check on my behalf.

  1. The old page turned away on its left edge. That is the default snapshot of the whole page, called root, given a keyframe animation that rotates it in perspective. The new page is already lying underneath.
  2. The word NOSCRIPT in the corner stayed put. It has a view-transition-name, so it is lifted out of the page snapshot and carried across separately.
  3. From the cover, the sheet you chose became the title plate. The fifth sheet in the stack and the plate at the top of this page share the name plate-5. The browser treats two elements with one name as a single object in two places and moves it from one to the other. Along the way it has to undo the tilt of the stack, and does so without being asked. Between two chapters there is no such pair, so the old plate steps aside and the new one slides in.
  1. ::view-transition-new(root)the page you reached
  2. ::view-transition-old(root)the page you left, turning
  3. ::view-transition-group(plate-5)a sheet becoming a plate
  4. ::view-transition-group(mark)staying where it is
The half second, at the speed of your scroll. These are the four pictures the browser holds above both documents during a page turn, stacked in the order it paints them. Scroll slowly. First they lift apart. Then the old page turns on its left edge, the plate travels from its place in the pile to its place on the page, and the mark does not move. The diagram holds the layers apart so that you can see them; on screen they are flat, and the old page never rises above the plate. Scroll back up and the page turn runs in reverse, which the real one never does.
The half second, held still. These are the four pictures the browser holds above both documents during a page turn, in the order it paints them. On a wide screen they are drawn as planes, frozen halfway: the old page part turned on its left edge, the plate midway between the pile and the page. With scroll-driven animations, and no request to reduce motion, scrolling plays the turn forwards and backwards.

To watch it again, go back to the contents and choose a sheet, or step sideways to page 04.

::view-transition
├─ ::view-transition-group(root)
│  └─ ::view-transition-image-pair(root)
│     ├─ ::view-transition-old(root)   the page you left, turning
│     └─ ::view-transition-new(root)   the page you reached
├─ ::view-transition-group(plate-5)    a sheet becoming a plate
└─ ::view-transition-group(mark)       staying where it is
The same thing as a tree. Each plane above is a pseudo-element the browser creates for the transition and removes afterwards. The rules in section 9 of the stylesheet are addressed to these names.

Where it stops

Firefox is the gap. It has had same-document view transitions since version 144, and those need a script to start them. The cross-document kind is behind a preference that is on in Nightly and off in the current release, Firefox 160. There, the pages of this zine simply load, as pages always have. That is the entire fallback, and it is why this is safe to ship: a browser that does not know the rule skips it.

Two more limits. The transition runs only between pages on the same origin. And it can only be as quick as the next page: making a page arrive early is the job of speculation rules, which are JSON inside a script element, so this zine does not use them. On a slow connection the pages here turn when the network lets them.

That is the last page. The cover has a table of everything used along the way and what your browser made of it, and a list of what a document still cannot do alone.