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
|
|
@ -172,6 +172,7 @@ export async function store(buf, { keepOriginal = false } = {}) {
|
|||
const tmp = path.join(UPLOADS_DIR, `.${filename}.tmp`);
|
||||
await writeFile(tmp, out);
|
||||
await rename(tmp, path.join(UPLOADS_DIR, filename));
|
||||
if (kind !== "svg") await writeThumb(filename, out);
|
||||
|
||||
return {
|
||||
filename,
|
||||
|
|
@ -183,16 +184,72 @@ export async function store(buf, { keepOriginal = false } = {}) {
|
|||
};
|
||||
}
|
||||
|
||||
/* Deletes one stored file. A file that's already gone is not an
|
||||
error: the row is what's being removed. */
|
||||
/* ── Small copies ──────────────────────────────────────────────
|
||||
Beside every raster upload, <name>-640.webp: the same image at most
|
||||
640px on its longer side. Photos and logos shown small (avatars,
|
||||
cards, marks) load it instead of the full file; the page falls back
|
||||
to the full file if it's missing (src/lib/media.ts, mediaThumb).
|
||||
Made when a file is uploaded, and at boot for any upload that
|
||||
doesn't have one yet (files from before this, or copied in by hand).
|
||||
───────────────────────────────────────────────────────────── */
|
||||
|
||||
export const THUMB_EDGE = 640;
|
||||
const THUMB = /-640\.webp$/;
|
||||
|
||||
export const thumbName = (filename) => `${filename.replace(/\.[^.]+$/, "")}-640.webp`;
|
||||
|
||||
async function writeThumb(filename, buf) {
|
||||
const name = thumbName(filename);
|
||||
const tmp = path.join(UPLOADS_DIR, `.${name}.tmp`);
|
||||
try {
|
||||
await sharp(buf, { animated: true, limitInputPixels: MAX_INPUT_PIXELS })
|
||||
.rotate()
|
||||
.resize({ width: THUMB_EDGE, height: THUMB_EDGE, fit: "inside", withoutEnlargement: true })
|
||||
.webp({ quality: WEBP_QUALITY })
|
||||
.toFile(tmp);
|
||||
await rename(tmp, path.join(UPLOADS_DIR, name));
|
||||
} catch (err) {
|
||||
// The full file is what matters; without a copy the page uses it.
|
||||
await unlink(tmp).catch(() => {});
|
||||
console.warn(`media: no small copy of ${filename}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/* Makes the small copy for every raster upload that lacks one.
|
||||
Returns how many it made. */
|
||||
export async function writeMissingThumbs() {
|
||||
let names;
|
||||
try {
|
||||
names = await readdir(UPLOADS_DIR);
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") return 0;
|
||||
throw err;
|
||||
}
|
||||
const present = new Set(names);
|
||||
let made = 0;
|
||||
for (const name of names) {
|
||||
if (name.startsWith(".") || THUMB.test(name) || /\.svg$/i.test(name)) continue;
|
||||
if (present.has(thumbName(name))) continue;
|
||||
const buf = await readFile(path.join(UPLOADS_DIR, name)).catch(() => null);
|
||||
if (!buf || !sniff(buf) || sniff(buf) === "svg") continue;
|
||||
await writeThumb(name, buf);
|
||||
made += 1;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
/* Deletes one stored file and its small copy. A file that's already
|
||||
gone is not an error: the row is what's being removed. */
|
||||
export async function remove(filename) {
|
||||
// filename comes from our own table, but it's joined onto a
|
||||
// directory, so refuse anything that isn't a bare name.
|
||||
if (path.basename(filename) !== filename) return;
|
||||
try {
|
||||
await unlink(path.join(UPLOADS_DIR, filename));
|
||||
} catch (err) {
|
||||
if (err.code !== "ENOENT") throw err;
|
||||
for (const name of [filename, thumbName(filename)]) {
|
||||
try {
|
||||
await unlink(path.join(UPLOADS_DIR, name));
|
||||
} catch (err) {
|
||||
if (err.code !== "ENOENT") throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -252,7 +309,7 @@ export async function registerLooseFiles(db) {
|
|||
|
||||
let added = 0;
|
||||
for (const name of names) {
|
||||
if (known.has(name) || name.startsWith(".") || path.basename(name) !== name) continue;
|
||||
if (known.has(name) || name.startsWith(".") || THUMB.test(name) || path.basename(name) !== name) continue;
|
||||
let row;
|
||||
try {
|
||||
row = await describe(name);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue