View Transitions
@farm.js/view-transitions animates client navigations with the browser's View Transitions
API. It starts a transition on navigation.loaded — once the route data is fetched but before
the router commits — and waits there until the browser calls the transition's update callback,
so the old snapshot is taken before the new page renders. It completes the transition on
navigation.resolved, once the new page is in place. The browser captures both snapshots and
animates between them.
Starting in navigation.loaded (rather than navigation.before) matters: the browser
suppresses rendering until the update callback settles, so starting before the fetch would
freeze the page for the whole route-data fetch — progress bars and useNavigation() UI could
not paint, Chromium aborts after about 4 seconds, and on cache hits the router could swap
the DOM before the browser captures the old page, so nothing animates.
Install
pnpm add @farm.js/view-transitionsComplete example
Register the plugin once in Farm config:
import { defineConfig } from "@farm.js/core";
import { viewTransitions } from "@farm.js/view-transitions";
export default defineConfig({
plugins: [viewTransitions()],
});That is the whole setup. Style the animation with the platform's pseudo-elements:
::view-transition-old(root) {
animation: 120ms ease-out fade-out;
}
::view-transition-new(root) {
animation: 180ms ease-in fade-in;
}Options
| Option | Default | Description |
|---|---|---|
exclude | [] | Route patterns that never animate, in Farm's file-route syntax: literals, [param] for one segment, [...rest] for the rest of the path, [[...rest]] for an optional rest — plus the legacy :param and trailing * shapes. Where the router supplies the matched route, patterns match its app-relative path (base path and locale prefix are NOT part of the match), so "/users/admin" excludes only that page, not every /users/[id] page; otherwise they match the destination pathname as shown in the address bar (base path and locale prefix included). |
viewTransitions({ exclude: ["/checkout", "/users/[id]", "/docs/[...slug]"] });Behavior
- The transition's update callback stays pending between
navigation.loadedandnavigation.resolved, so the new snapshot is the page the router actually rendered. Streamed and islands navigations settle on the sameresolvedsignal, so their content is what gets captured. The page is frozen only during the commit, never during the fetch. - Browsers without
document.startViewTransitionnavigate exactly as before; the plugin is a progressive enhancement and never gates a navigation on the API. prefers-reduced-motiondisables the animation. The navigation still happens.- A failed navigation (
navigation.error) and a cancelled one (itssignalaborting, e.g. a hash link clicked mid-navigation) release the pending update callback, so a callback can never hang.cancelActiveNavigationin the SPA router aborts the plugin session for the same reason. - Refreshes never animate:
router.refresh()and the refresh after a server action re-render the page on screen, which would otherwise cross-fade into itself. - Configure view transitions in one
viewTransitions()instance per app.
Working with core's viewTransition option
Core's Link and navigateTo already take a per-navigation viewTransition option, which
wraps that navigation's commit in document.startViewTransition itself. With both on, a
navigation would start two transitions. The plugin therefore stands aside whenever the
navigation carries that option — core owns the transition, the plugin starts none:
import { Link } from "@farm.js/core/client";
// Animates via core's option; the plugin does not start a second transition.
<Link href="/gallery" viewTransition>
Gallery
</Link>;The same applies to a plain anchor with data-view-transition="true", and to
navigateTo("/gallery", { viewTransition: true }). Use the plugin for the app-wide default
and the option for one-off navigations.
Limits
- The plugin ships no CSS. Without your own
::view-transitionrules, the browser's default cross-fade applies. - Only client navigations animate. The first page load and full document reloads do not run the client plugin hooks, so they are never wrapped.
Animate client navigations with the View Transitions API: start on navigation.loaded, complete on navigation.resolved, with per-route opt-out.