When you click a game on the richi.codes home shelf, its cover lifts off with an orange glow and flies into the player on /juego/<slug>, turning from landscape into portrait on the way. That is one same-document View Transition with one shared name, hooked into a framework that doesn't ship it: the Next.js pages router. The demo above is a standalone copy of the same technique, with the three things that went wrong along the way as switches you can flip.
What you see, and what does it
Same-document view transitions work like this: you call document.startViewTransition(callback), the browser takes a snapshot of the page, runs your callback to change the DOM, takes a second snapshot, and animates between the two. Any element with a view-transition-name gets its own group that moves and resizes from its old box to its new one. Chrome's guide calls this the list-to-detail pattern; on richi.codes the list is the shelf and the detail is the player.
On the site it all lives in lib/pageTransitions.js (134 lines, most of them comments) and a block of styles/globals.css. No library.
Where to hook into Next.js
next/link and the site search both navigate with router.push, which the Next.js docs describe as the method that "handles client-side transitions". So the hook wraps the real router's push, and the page change happens inside the transition:
const push = router.push;
router.push = function (url, as, options = {}) {
return new Promise((resolve, reject) => {
router.prefetch(url, as, options).catch(() => {}).then(() => {
const vt = document.startViewTransition(() => {
const done = push.call(router, url, as, options);
done.then(resolve, reject);
return done.catch(() => {});
});
vt.finished.finally(clearNames);
});
});
};
The first attempt was different, according to the note Dr. Kernel left with this change: start the transition on routeChangeStart and finish it on routeChangeComplete. With the page already prefetched, React painted the new page before the browser took the "before" snapshot, the promise never resolved, and the transition ended up skipped by a timeout. Putting the push inside the callback keeps the order right: snapshot, DOM change, snapshot.
Two guards sit around it:
- Prefetch first. Chrome's guide warns that while the update callback runs "the page is frozen", and that "network fetches should be done before calling
.startViewTransition()". The spec lists it plainly in the lifecycle: rendering is paused until the new state is ready. So the hook callsrouter.prefetch()before starting, and if that takes more than 1.2 seconds it navigates without animation. A second timer callsskipTransition()if painting the new page is slow; MDN says it skips the animation "but doesn't skip running the associated view update". - Only where it makes sense. No API,
prefers-reduced-motion: reduce, shallow routing or back/forward (whichpushdoesn't handle) all fall through to the normalpush.
One name on each side, set at the last moment
A name has to be unique. MDN: "If two rendered elements have the same view-transition-name at the same time, ViewTransition.ready will reject and the transition will be skipped." The shelf has many covers, so they can't all carry the name in CSS. Tick Bug: name every cover in the demo to see what happens: the log shows ready rejected with an InvalidStateError (the spec's error for a failed capture), the view still changes, and nothing flies.
The fix the Modern Web Guidance calls the "dynamic list item": the player carries the name in CSS, and the clicked cover gets it from JavaScript just before the transition starts and loses it when it ends.
// capture phase: runs before next/link's own onClick
document.addEventListener('click', (e) => {
clicked = e.target.closest('a[href]')?.querySelector('[data-vt-cover]');
}, true);
// right before startViewTransition()
setName(clicked, 'game-stage'); // el.style.viewTransitionName = …
// on vt.finished: every hand-set name is removed
There is one extra trap on the site: going from one game to another, the starting page already has a player called game-stage. The hook sets that one to none first, so there is still only one name on each side.
No stretching: height 100% plus object-fit
The cover is 1200:630 and the player is portrait. The snapshots behave like replaced elements; the spec says they "can be manipulated with object-* properties in the same way that other replaced elements can be". By default, Chrome's guide explains, "the old and new views are 100% width of the group, and auto height", so each keeps its own ratio and neither fits the travelling box. Switch the demo's snapshots control between the three options to compare:
::view-transition-image-pair(game-stage) {
border-radius: 16px;
overflow: clip; /* the box crops its contents */
}
::view-transition-old(game-stage),
::view-transition-new(game-stage) {
height: 100%; /* fill the group… */
object-fit: cover; /* …without distorting */
overflow: clip;
}
default leaves the landscape snapshot floating at the top, height:100% alone squashes it, and with object-fit: cover both images are cropped like the real cover is.
A short hand-over instead of a long cross-fade
The default cross-fade runs for the whole trip and blends the two images with mix-blend-mode: plus-lighter. With two different crops of the same art, that leaves both visible on top of each other for half the animation. richi.codes swaps them early and fast, from 12% to 38% of the trip, with normal blending:
@keyframes stage-old { 0%, 12% { opacity: 1; } 38%, 100% { opacity: 0; } }
@keyframes stage-new { 0%, 12% { opacity: 0; } 32%, 100% { opacity: 1; } }
Turn on Slow motion and switch cross-fade between "short relay" and "browser default" to see the difference. The trip takes 600 ms with cubic-bezier(.65,0,.35,1), the old page fades in 180 ms and the new one rises 12 px in 440 ms.
Browser support and gotchas
- Support: same-document View Transitions are Baseline newly available since October 14, 2025: Chrome and Edge 111, Safari 18, Firefox 144. Without the API the site navigates as always, and the demo shows a notice.
- Focus isn't moved for you. The Modern Web Guidance marks it as mandatory: move focus after the transition. The demo focuses the game title on the way in and the cover it came from on the way out.
- Don't wait inside the callback. Anything slow there freezes the screen. Prefetch before, and keep a way out with
skipTransition(). - Clean up names. A hand-set name left behind becomes a duplicate on the next navigation.
What to keep an eye on
- Cross-document transitions. The
@view-transitionrule animates real page loads without a router, but web-features still lists it as limited availability (Chrome, Edge and Safari, not Firefox), so it isn't a replacement here yet. - "Widely available". Baseline newly available means every engine ships it, not that every visitor has updated. The fallback costs nothing, so it stays.