Make the site faster to load and lighter on the server
- Every page but Home, and the whole admin, loads on first visit: the main bundle drops from 579 kB to 314 kB (173 to 98 kB gzipped); a tab on an old build reloads once when a page chunk has gone - Home asks for events and the timeline alongside its own data, and the server preloads /api/front-page and the first hero photo - Hero headline rises in as one piece (it held back the page's largest paint), sized to stay within two lines; min-h in dvh; first slide fetched at high priority - Every raster upload gets a 640px copy (at upload, and at boot for older files); logos, avatars and card pictures load it and fall back to the full file - Login password checks run off the event loop; only admin requests are logged per request; ffmpeg runs at nice 19 on Linux - Page HTML is no-cache, so a deploy can't strand a cached page on missing assets; source maps are no longer linked - Migration 042: index person_awards by award - docs/performance.md, and gzip and HTTP/2 for nginx Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
be7e92417e
commit
f72a9604d2
23 changed files with 420 additions and 139 deletions
36
src/lib/useSmallImage.ts
Normal file
36
src/lib/useSmallImage.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
useSmallImage
|
||||
|
||||
For an upload shown small (an avatar, a logo, a card picture):
|
||||
try its 640px copy first (<name>-640.webp, which the server makes
|
||||
beside every raster upload, server/src/media.js), then the full
|
||||
file if the copy isn't there, then give up so the caller can draw
|
||||
its fallback. Anything that isn't an upload, and SVGs, go straight
|
||||
to the full file.
|
||||
|
||||
const img = useSmallImage(mediaUrl(person.photo))
|
||||
img.failed ? <Initials /> : <img src={img.src} onError={img.onError} />
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
/* '/uploads/abc.webp' → '/uploads/abc-640.webp'; null for anything else. */
|
||||
export function smallCopy(src?: string | null): string | null {
|
||||
if (!src || !src.startsWith('/uploads/') || /\.svg(\?|#|$)/i.test(src)) return null
|
||||
return src.replace(/\.[^./?#]+(?=$|[?#])/, '') + '-640.webp'
|
||||
}
|
||||
|
||||
export function useSmallImage(src?: string | null) {
|
||||
// 0: the small copy (when there is one), 1: the full file, 2: failed
|
||||
const [stage, setStage] = useState(0)
|
||||
useEffect(() => setStage(0), [src])
|
||||
|
||||
const small = smallCopy(src)
|
||||
const current = !src ? null : stage === 0 && small ? small : stage <= 1 ? src : null
|
||||
|
||||
return {
|
||||
src: current ?? undefined,
|
||||
failed: current === null,
|
||||
onError: () => setStage((s) => (s === 0 && small ? 1 : 2)),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue