From 747e6e25e00ac4d7c4b6b44b90f26ee048c214b2 Mon Sep 17 00:00:00 2001
From: blake
Date: Tue, 20 Oct 2026 13:17:27 -0500
Subject: [PATCH] feat: smooth same-origin page changes
Cache versioned styles, images, and scripts so repeat clicks do not wait on them, crossfade navigations while the chrome stays put, and prefetch ordinary links on hover.
---
public/css/app.css | 18 +++++
src/admin-pages.tsx | 9 +--
src/app.tsx | 39 +++++------
src/pages.tsx | 11 +--
src/security.ts | 5 +-
src/static-assets.ts | 134 +++++++++++++++++++++++++++++++++++++
tests/admin.test.ts | 2 +-
tests/public-pages.test.ts | 54 +++++++++++++--
8 files changed, 236 insertions(+), 36 deletions(-)
create mode 100644 src/static-assets.ts
diff --git a/public/css/app.css b/public/css/app.css
index a8672b0..30884af 100644
--- a/public/css/app.css
+++ b/public/css/app.css
@@ -170,6 +170,11 @@ a.btn:active {
color: var(--hope-ink);
}
+/* Same-origin clicks crossfade. Named chrome stays in place while the page changes. */
+@view-transition {
+ navigation: auto;
+}
+
.header {
position: relative;
font-size: 18px;
@@ -179,6 +184,7 @@ a.btn:active {
}
.nav {
+ view-transition-name: site-header;
left: 0;
position: fixed;
z-index: 999;
@@ -298,6 +304,7 @@ a.btn:active {
}
.session-bar {
+ view-transition-name: session-bar;
position: fixed;
z-index: 1000;
top: 118px;
@@ -1921,6 +1928,7 @@ button.btn-quiet::before {
/* Footer. Pixel sizes match hopefoodpantrysc.org. */
.footer {
+ view-transition-name: site-footer;
position: relative;
display: flex;
flex-direction: column;
@@ -2701,6 +2709,16 @@ button.btn-quiet::before {
}
@media (prefers-reduced-motion: reduce) {
+ @view-transition {
+ navigation: none;
+ }
+
+ ::view-transition-group(*),
+ ::view-transition-old(*),
+ ::view-transition-new(*) {
+ animation: none !important;
+ }
+
html {
scroll-behavior: auto;
}
diff --git a/src/admin-pages.tsx b/src/admin-pages.tsx
index f2ac6a5..fd1ae63 100644
--- a/src/admin-pages.tsx
+++ b/src/admin-pages.tsx
@@ -16,6 +16,7 @@ import {
import type { SignupErrors, SignupValues } from './signup-form.js';
import { addLocalDays, todayInZone } from './time.js';
import { ShiftTargetOptions } from './shift-options.js';
+import { assetHref } from './static-assets.js';
import { appPath } from './url.js';
export type WalkInState = {
@@ -243,12 +244,12 @@ export function AdminHome(props: {
/>
-
+
>
);
}
@@ -877,7 +878,7 @@ function ConfirmDialog(props: { config: AppConfig; csrf: string; prompt?: Confir
-
+
>
);
}
diff --git a/src/app.tsx b/src/app.tsx
index 426cb12..7afcf61 100644
--- a/src/app.tsx
+++ b/src/app.tsx
@@ -1,6 +1,3 @@
-import path from 'node:path';
-import { fileURLToPath } from 'node:url';
-import { serveStatic } from '@hono/node-server/serve-static';
import type Database from 'better-sqlite3';
import { Hono, type Context } from 'hono';
import { bodyLimit } from 'hono/body-limit';
@@ -14,12 +11,11 @@ import { privacyParagraphs, readPantrySettings } from './settings.js';
import { registerReminderOff } from './reminder-http.js';
import { registerPublicSignup } from './signup-http.js';
import { securityHeaders } from './security.js';
+import { SHARED_ASSET_CACHE, SPECULATION_RULES, assetCacheControl, publicDirectory, readPublicFile, rewriteCssUrls, staticAssetStamp } from './static-assets.js';
import { nowUtc } from './time.js';
import { appPath } from './url.js';
-export function publicDirectory(): string {
- return path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../public');
-}
+export { publicDirectory };
export function createApp(
config: AppConfig,
@@ -35,7 +31,7 @@ export function createApp(
const now = () => options?.now ?? nowUtc();
const publicDir = options?.publicDir ?? publicDirectory();
- app.use('*', securityHeaders({ https: config.secureCookies }));
+ app.use('*', securityHeaders({ https: config.secureCookies, speculationRules: appPath(config, '/speculation-rules.json') }));
app.use('*', bodyLimit({
maxSize: 64 * 1024,
onError: (c) => {
@@ -48,6 +44,12 @@ export function createApp(
app.use(appPath(config, '/img/*'), staticFiles(config, publicDir));
app.use(appPath(config, '/js/*'), staticFiles(config, publicDir));
+ app.get(appPath(config, '/speculation-rules.json'), (c) => {
+ c.header('Cache-Control', SHARED_ASSET_CACHE);
+ c.header('Content-Type', 'application/speculationrules+json; charset=utf-8');
+ return c.body(JSON.stringify(SPECULATION_RULES));
+ });
+
app.get(appPath(config, '/healthz'), (c) => {
c.header('Cache-Control', 'no-store');
try {
@@ -150,17 +152,16 @@ function page(
}
function staticFiles(config: AppConfig, publicDir: string) {
- return serveStatic({
- root: publicDir,
- onFound: (_path, c) => {
- c.header('Cache-Control', 'no-cache');
- },
- rewriteRequestPath: (requestPath) => {
- if (config.basePath !== '' && requestPath.startsWith(config.basePath)) {
- return requestPath.slice(config.basePath.length) || '/';
- }
- return requestPath;
- },
- });
+ return async (c: Context, next: () => Promise) => {
+ const file = readPublicFile(publicDir, c.req.path, config.basePath);
+ if (!file) return next();
+ const cache = assetCacheControl(c.req.query('v'), publicDir);
+ c.header('Cache-Control', cache);
+ c.header('Content-Type', file.type);
+ if (file.type.startsWith('text/css')) {
+ return c.body(rewriteCssUrls(file.body.toString('utf8'), staticAssetStamp(publicDir)));
+ }
+ return c.body(new Uint8Array(file.body));
+ };
}
diff --git a/src/pages.tsx b/src/pages.tsx
index 3e17a34..8f7b861 100644
--- a/src/pages.tsx
+++ b/src/pages.tsx
@@ -6,12 +6,13 @@ import type { SignupErrors, SignupValues } from './signup-form.js';
import type { MoveTarget } from './admin/store.js';
import { ShiftTargetOptions } from './shift-options.js';
import type { ManageView } from './signup.js';
+import { assetHref } from './static-assets.js';
import { appPath } from './url.js';
const PANTRY_SITE = 'https://hopefoodpantrysc.org';
function asset(config: AppConfig, file: string): string {
- return appPath(config, file);
+ return assetHref(config, file);
}
function pathIs(path: string | undefined, target: string): boolean {
@@ -45,9 +46,9 @@ export function Layout(props: {
{props.title}
-
-
-
+
+
+
@@ -208,7 +209,7 @@ export function Layout(props: {
{props.turnstile ? : null}
-
+