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.
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();
});
@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.
-
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. -
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. -
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.
-
::view-transition-new(root)the page you reached -
::view-transition-old(root)the page you left, turning -
::view-transition-group(plate-5)a sheet becoming a plate -
::view-transition-group(mark)staying where it is
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
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.