View Transitions Migration Design
Problem
Almanack’s frontpage navigation uses HTMX to swap #article-swap in place, preserving the masthead and side margins across navigation. The mechanism works, but its bug log shows a recurring pattern: state that doesn’t tear down cleanly, state that doesn’t re-init cleanly, and history snapshots that capture the wrong DOM.
A sample of the bug taxonomy from git log:
| Category | Commits |
|---|---|
| Duplicate widgets / stale handlers on swap or back-forward | 32f88b1 da2099c 531c297 29c16bd 2e3a6e8 |
| History-snapshot quirks (HTMX’s own history mechanism) | 00b2dd2 d4fb322 9846225 32f88b1 |
| Swap boundary errors | de16172 f77b423 |
| Re-init plumbing for individual subsystems | 5b2ba58 95cf7c3 7107abd 51fe417 bbe3e0b |
Root cause: HTMX forces the codebase to maintain two parallel lifecycles — initial page load vs. swap re-init — and bugs grow in the gap between them. Each interactive subsystem (sidenotes, margin boxes, pen marginalia, celestial) has to know how to tear itself down on htmx:beforeSwap and rebuild on htmx:afterSettle, and the back/forward path adds further forks for htmx:beforeHistorySave / htmx:historyRestore.
Solution
Replace HTMX with cross-document View Transitions. The browser handles fetch, parse, swap, and animation; same-origin navigation gets a smooth transition by adding one CSS rule:
@view-transition { navigation: auto; }
No JS swap orchestration. No teardown/reinit choreography. Every page load is a fresh init, which is the lifecycle the codebase already handles correctly.
Browser support (May 2026)
| Browser | Cross-document VTs |
|---|---|
| Chrome / Edge | Shipped, 126+ (June 2024) |
| Safari | Shipped, 18.2+ (Dec 2024), full by 18.5 |
| Firefox | Partial (146-151); full support is an Interop 2026 deliverable |
Firefox users currently fall through to plain navigation — a flash instead of a transition, no behavioural regression. By the time Stage 2 lands, Firefox is likely shipping full support.
Drop the celestial sweep
The current celestial.js sweep animation across date navigation (sun/moon arcing between dates) is the only piece of behaviour that cannot be expressed naturally with View Transitions. Preserving it would require either passing animation state through view-transition-name snapshots or stashing the old date in sessionStorage so pagereveal can animate from it.
We’re choosing to drop the sweep and let the browser cross-fade the celestial canvas like any other element. Rationale:
- It’s gaudy on repeated viewings (per the editor)
- It costs ~120 lines of date-animation state machine in
celestial.js(lines ~1679-1800) - It costs the
__celestialSetDatecross-module API and its orchestration infrontpage-nav.js - Cross-fade is on-brand for the antique-print aesthetic — closer to a page-turn than an orrery
Affected components
Direct HTMX dependents (5 production files):
| File | Today | After migration |
|---|---|---|
src/almnck/templates/base.html:65 |
<script src="htmx.min.js"> |
Removed |
assets/js/frontpage-nav.js (307 lines) |
Wires hx-* attrs onto prev/next/today; orchestrates post-swap callbacks |
Deleted |
assets/js/sidenotes.js:2367, 2378, 2387 |
Hooks htmx:afterSwap, beforeHistorySave, historyRestore |
Migrated to pageshow / pagehide |
assets/js/interactive-margin-boxes.js:75-78 |
Tears down + re-inits on swap | Hooks removed, runs on load only |
assets/js/pen-marginalia.js:340, 346 |
Tears down + re-inits on swap | Hooks removed, runs on load only |
assets/js/htmx.min.js |
Vendored library | Deleted |
Indirect dependents (called by frontpage-nav.js but no HTMX import):
| File | Today | After migration |
|---|---|---|
assets/js/celestial.js:1683 |
Exposes __celestialSetDate; ~120 lines of sweep animation |
API deleted; sweep block deleted; canvas cross-fades naturally |
assets/js/zodiac.js |
__zodiacRender called from celestial sweep |
Untouched (still called from doRender) |
Subsystems with no HTMX coupling that nevertheless benefit:
assets/js/figures/runtime.js: invoked from inline<script>blocks inside article markdown. Inline-script re-execution under HTMX swaps has been a quirky area; full-page navigation runs them unconditionally, removing a latent bug class.- Margin box
code.jsmodules (editorial/margin-boxes/*/assets/code.js): managed entirely byinteractive-margin-boxes.js. Thesetup(container)contract is unchanged. topup.js,index-search.js,identity.js:DOMContentLoaded-init only, no swap awareness, no changes needed.
Architecture changes
Navigation
Today: prev/next/today links get HTMX attributes wired on by frontpage-nav.js. Disabled links get them unwired. htmx.process() syncs internal state.
After: prev/next/today links are plain <a href>. The browser navigates. View Transitions cross-fade #article-swap (and anything else without a view-transition-name).
History / back-forward
Today: HTMX snapshots partial HTML into its own history store. htmx:beforeHistorySave lets sidenotes.js save placedState (occupied ranges + element heights) before the snapshot. htmx:historyRestore restores it on back/forward.
After: Browser bfcache handles back/forward. pagehide fires before the page enters bfcache (use event.persisted to distinguish); pageshow fires when restored (also has event.persisted). placedState is saved on pagehide, restored on pageshow. The plumbing is shorter and uses standard browser APIs.
Margin box / pen marginalia lifecycle
Today: each module tracks per-element cleanup functions, calls them on htmx:beforeSwap, re-inits on htmx:afterSettle. Margin boxes additionally hoist <style> blocks above the container to survive innerHTML replacement.
After: each module runs once on DOMContentLoaded. Cleanup functions are no longer needed; the old document dies with the navigation. The <style> hoist becomes vestigial.
Celestial canvas
Today: canvas lives in the masthead (outside #article-swap), survives the swap, animates between dates via __celestialSetDate(newDate) called from frontpage-nav.js after each swap.
After: canvas is a fresh instance per navigation. It initializes from data-date on #article-swap exactly as it does on first load. No date animation; the browser cross-fades the canvas pixels.
Two-stage rollout
Stage 1 — Additive. Add @view-transition { navigation: auto } and ship cross-document VTs alongside HTMX. Links remain HTMX-wired; cross-document transitions handle the cases where HTMX doesn’t intercept (article-to-article navigation, direct URL entry, external links into the site). This proves the VT path works in production without removing the fallback.
Stage 2 — Removal. Once Stage 1 has soaked, delete frontpage-nav.js, delete htmx.min.js, delete the script tag, delete the celestial sweep, migrate sidenotes’ history hooks to pageshow/pagehide, drop HTMX hooks in interactive-margin-boxes.js and pen-marginalia.js. Rewrite tests.
Rationale for splitting: lets us bisect regressions to the additive change vs. the removal. Also gives time for Firefox’s cross-doc VT support to mature in the field before HTMX is gone.
Tests
Three pytest files exercise HTMX behaviour directly:
src/tests/test_htmx_rapid_nav.py— race conditions across rapid prev/next clicks. Rewrite to use plain browser navigation; the test for “rapid clicks don’t corrupt state” is still valuable, just expressed differently.src/tests/test_layout_lifecycle.py— verifies sidenotes/margin-box teardown and re-init order. Rewrite againstpageshow/pagehide(most of the lifecycle goes away; what remains is bfcache restoration).src/tests/test_layout_stress.py— stress test simulatingouterHTMLswaps (f77b423). Delete in Stage 2 — its premise (HTMX swaps) is gone.
New tests in Stage 1: - Cross-document VT smoke test (feature-detect, verify CSS rule is in place) - Article-to-article navigation preserves expected state
New tests in Stage 2:
- pageshow/pagehide history restore for sidenotes layout
- No HTMX library loaded
- No hx-* attributes anywhere in rendered HTML
Risks and open questions
-
bfcache eligibility. Browsers refuse to bfcache pages with
unloadlisteners, open IndexedDB connections, etc. Need to verify Almanack pages stay bfcache-eligible. Quick check: open DevTools → Application → Back/Forward Cache. -
Scroll restoration. HTMX’s history mechanism handled scroll position. bfcache also does, but the boundary case (forward to a page never visited) falls back to browser default. Confirm behaviour matches expectations.
-
Inline
<script>in article markdown. Several articles bootstrap figures via inline scripts (e.g.,editorial/articles/regolith-with-latitude/article.md:60-61). These work correctly on full page loads (Stage 2). Stage 1’s HTMX path keeps existing behaviour; nothing changes. -
prefers-reduced-motion. View Transitions respect it automatically, but worth confirming in test. -
No-JS readers. Plain
<a href>works without JS — already a Stage 2 improvement over today’s HTMX-required nav. Worth re-checking that the chronology archive (/chronology/) remains the documented JS-free fallback.
Out of scope
- Same-document
document.startViewTransition()calls for in-page state changes (the article body never mutates client-side; nothing to wrap). - Named element transitions (
view-transition-nameon title, masthead, etc.). Plain crossfade only for the first cut. Could be added later as polish. - Page transitions for the editorial CMS / admin views.
Decision log
- 2026-05-19: Drop the celestial sweep animation rather than preserve it across the migration. Editor’s call: gaudy on repeated viewings, not worth the ~120 lines of state-machine and cross-module orchestration to keep.
- 2026-05-19: Two-stage rollout (additive then removal) chosen over one big PR for bisectability and to let Firefox cross-doc VT support mature.
- 2026-05-19: Plain crossfade only for first cut; named transitions deferred.