Commit 4503d77b by PLN (Algolia)

404: sketch assets stored static — backgrounds survive every visitor

scripts/hydra-assets.mjs pulls each StarryNights piece's initImage
assets (file://, absolute and git-raw URL shapes normalized) into
public/images/hydra/ with a rewrite manifest; 16M, 21 assets, Webb
downscaled. app/not-found.js applies the manifest to the embedded
sources at build — the git-raw originals were cross-origin and
case-fragile, which is why backgrounds vanished for some visitors.
Copy: l'Espace entre les Pages.
parent e41b29f8
...@@ -12,9 +12,19 @@ export const metadata = { ...@@ -12,9 +12,19 @@ export const metadata = {
/** The void is a gallery: every StarryNights sketch in content/hydras is /** The void is a gallery: every StarryNights sketch in content/hydras is
* resolved at build (same pipeline as /hydra/[id]) and embedded, so each * resolved at build (same pipeline as /hydra/[id]) and embedded, so each
* visit boots one of the real pieces — never the same twice in a row. */ * visit boots one of the real pieces — never the same twice in a row.
* Asset refs are rewritten to the static copies in /images/hydra (see
* scripts/hydra-assets.mjs): the git-raw originals are cross-origin and
* case-fragile, and backgrounds vanished for some visitors. */
function withLocalAssets(source, rewrites) {
let out = source;
for (const [ref, local] of Object.entries(rewrites)) out = out.split(ref).join(local);
return out;
}
async function loadSketches() { async function loadSketches() {
const dir = path.join(process.cwd(), 'content', 'hydras'); const dir = path.join(process.cwd(), 'content', 'hydras');
const manifest = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'public/images/hydra/manifest.json'), 'utf8'));
const out = []; const out = [];
for (const f of fs.readdirSync(dir).filter((name) => name.endsWith('.md'))) { for (const f of fs.readdirSync(dir).filter((name) => name.endsWith('.md'))) {
const { data } = matter(fs.readFileSync(path.join(dir, f), 'utf8')); const { data } = matter(fs.readFileSync(path.join(dir, f), 'utf8'));
...@@ -23,7 +33,7 @@ async function loadSketches() { ...@@ -23,7 +33,7 @@ async function loadSketches() {
if (sketch) { if (sketch) {
out.push({ out.push({
title: sketch.credits?.title || data.title || f.replace(/\.md$/, ''), title: sketch.credits?.title || data.title || f.replace(/\.md$/, ''),
source: sketch.source, source: withLocalAssets(sketch.source, manifest[f.replace(/\.md$/, '')] ?? {}),
}); });
} }
} }
...@@ -47,7 +57,7 @@ export default async function NotFound() { ...@@ -47,7 +57,7 @@ export default async function NotFound() {
404 404
</h1> </h1>
<p className="mt-6 text-lg text-[var(--text-primary)] text-balance [text-shadow:0_1px_12px_var(--surface)]"> <p className="mt-6 text-lg text-[var(--text-primary)] text-balance [text-shadow:0_1px_12px_var(--surface)]">
Vous êtes dans l’espace entre les pages. Vous êtes dans l’Espace entre les Pages.
</p> </p>
</main> </main>
</Link> </Link>
......
{
"adenora": {
"https://git.plnech.fr/pln/Hydra/raw/master/StarryNights/img/august-15-2019-triangulum-galaxy.png": "/images/hydra/adenora-august-15-2019-triangulum-galaxy.png",
"https://git.plnech.fr/pln/Hydra/raw/master/StarryNights/img/adenora.png": "/images/hydra/adenora-adenora.png"
},
"ariane13": {
"/home/pln/Work/Hydra/StarryNights/img/april-13-2019-galaxy-cluster-abell-2261.jpg": "/images/hydra/ariane13-april-13-2019-galaxy-cluster-abell-2261.jpg",
"https://git.plnech.fr/pln/Hydra/raw/e8cd9c3651615342ee7af334fe21684585409941/StarryNights/img/april-13-2019-galaxy-cluster-abell-2261.jpg": "/images/hydra/ariane13-april-13-2019-galaxy-cluster-abell-2261.jpg"
},
"camille15": {
"https://git.plnech.fr/pln/Hydra/raw/850a3aa96138ec40b71b5e57f328dba9c8f872f5/StarryNights/img/october-15-2019-supernova.jpg": "/images/hydra/camille15-october-15-2019-supernova.jpg"
},
"caoimhe": {
"/home/pln/Work/Hydra/StarryNights/img/march-4-2019-galaxy-fornax-a.jpg": "/images/hydra/caoimhe-march-4-2019-galaxy-fornax-a.jpg",
"https://git.plnech.fr/pln/Hydra/raw/fadf3c87cb23d0e933e30962f6e2d530e2ab483a/StarryNights/img/march-4-2019-galaxy-fornax-a.jpg": "/images/hydra/caoimhe-march-4-2019-galaxy-fornax-a.jpg"
},
"cosmos": {
"file:///home/pln/Work/Hydra/StarryNights/img/september-28-2019-galaxy-ngc-300.jpg": "/images/hydra/cosmos-september-28-2019-galaxy-ngc-300.jpg",
"https://git.plnech.fr/pln/Hydra/raw/eecd6aaf74b4af18af9af22a4eed8d6b201d4824/StarryNights/img/september-28-2019-galaxy-ngc-300.jpg": "/images/hydra/cosmos-september-28-2019-galaxy-ngc-300.jpg",
"file:///home/pln/Work/Hydra/StarryNights/img/cosmos.jpg": "/images/hydra/cosmos-cosmos.jpg",
"https://git.plnech.fr/pln/Hydra/raw/eecd6aaf74b4af18af9af22a4eed8d6b201d4824/StarryNights/img/cosmos.jpg": "/images/hydra/cosmos-cosmos.jpg"
},
"didier": {
"file:///home/pln/Work/Hydra/StarryNights/img/february-24-2019-saturn-and-moons.jpg": "/images/hydra/didier-february-24-2019-saturn-and-moons.jpg"
},
"dusty-halley": {
"https://git.plnech.fr/pln/Hydra/raw/master/StarryNights/img/halley.png": "/images/hydra/dusty-halley-halley.png"
},
"fantome": {},
"full_moon": {
"https://git.plnech.fr/pln/Hydra/raw/master/biarritz/fullmoon.avif": "/images/hydra/full_moon-fullmoon.avif"
},
"galactic": {
"file:///home/pln/Work/Hydra/StarryNights/img/724203.jpg": "/images/hydra/galactic-724203.jpg"
},
"glitchpool": {},
"karine": {
"https://git.plnech.fr/pln/Hydra/raw/master/StarryNights/img/august-15-2019-triangulum-galaxy.png": "/images/hydra/karine-august-15-2019-triangulum-galaxy.png",
"https://git.plnech.fr/pln/Hydra/raw/master/StarryNights/img/adenora.png": "/images/hydra/karine-adenora.png"
},
"lucia": {},
"march-4": {
"file:///home/pln/Work/Hydra/StarryNights/img/february-24-2019-saturn-and-moons.jpg": "/images/hydra/march-4-february-24-2019-saturn-and-moons.jpg"
},
"petrole": {},
"pierre_feuilles": {},
"planche": {},
"poussiere_dor": {},
"square-worlds": {
"https://git.plnech.fr/pln/Hydra/raw/7ab247cae7f562ecc0f1c13a6646e5a71f6c0f1f/StarryNights/img/724203.jpg": "/images/hydra/square-worlds-724203.jpg"
},
"sylvie": {
"file:///home/pln/Work/Hydra/StarryNights/img/february-18.jpg": "/images/hydra/sylvie-february-18.jpg"
},
"tom24": {
"file:///home/pln/Work/Hydra/StarryNights/img/august-10-2019-nebula-ngc-2074.jpg": "/images/hydra/tom24-august-10-2019-nebula-ngc-2074.jpg",
"https://git.plnech.fr/pln/Hydra/raw/c4bdfe763713f6eac92f474b4cd815f840e92dbf/StarryNights/img/august-10-2019-nebula-ngc-2074.jpg": "/images/hydra/tom24-august-10-2019-nebula-ngc-2074.jpg",
"file:///home/pln/Work/Hydra/opal/opal.jpeg": "/images/hydra/tom24-opal.jpeg",
"https://git.plnech.fr/pln/Hydra/raw/c4bdfe763713f6eac92f474b4cd815f840e92dbf/opal/opal.jpeg": "/images/hydra/tom24-opal.jpeg",
"file:///home/pln/Work/Hydra/opal/opal2.jpeg": "/images/hydra/tom24-opal2.jpeg",
"https://git.plnech.fr/pln/Hydra/raw/c4bdfe763713f6eac92f474b4cd815f840e92dbf/opal/opal2.jpeg": "/images/hydra/tom24-opal2.jpeg"
},
"tulipes": {
"/home/pln/Work/Hydra/img/tulipes.jpeg": "/images/hydra/tulipes-tulipes.jpeg",
"https://git.plnech.fr/pln/Hydra/img/tulipes.jpeg": "/images/hydra/tulipes-tulipes.jpeg"
},
"yet": {
"StarryNights/img/Webb_deepest_yet.png": "/images/hydra/yet-Webb_deepest_yet.jpg"
}
}
#!/usr/bin/env node
/**
* Store every StarryNights sketch's image assets statically.
*
* The /hydra sketches live in git.nech.pl/pln/Hydra and reference images
* via initImage("file:///home/pln/Work/Hydra/<proj>/<file>") (rewritten for
* the web to git-raw URLs by the sanitizer — cross-origin and case-fragile,
* which is why backgrounds vanish for some visitors). This script:
*
* 1. fetches each sketch source (the content/hydras frontmatter URL);
* 2. pulls every initImage asset into public/images/hydra/<sketch>/;
* 3. writes a rewrite manifest (original ref → local path) that
* app/not-found.js applies to the embedded sources at build.
*
* Idempotent: existing files are kept (delete the folder to refresh).
* Run it whenever a sketch gains new assets: `yarn node scripts/hydra-assets.mjs`.
*/
import fs from 'node:fs';
import path from 'node:path';
import matter from 'gray-matter';
const HYDRAS = path.resolve(process.cwd(), 'content/hydras');
const OUT = path.resolve(process.cwd(), 'public/images/hydra');
const MANIFEST = path.join(OUT, 'manifest.json');
const RAW = 'https://git.plnech.fr/pln/Hydra/raw/master/';
const sketchUrls = fs
.readdirSync(HYDRAS)
.filter((f) => f.endsWith('.md'))
.map((f) => {
const id = f.replace(/\.md$/, '');
const { data } = matter(fs.readFileSync(path.join(HYDRAS, f), 'utf8'));
return { id, url: data.source };
})
.filter((s) => s.url);
fs.mkdirSync(OUT, { recursive: true });
const manifest = fs.existsSync(MANIFEST) ? JSON.parse(fs.readFileSync(MANIFEST, 'utf8')) : {};
let fetched = 0;
let failed = 0;
async function download(url, dest) {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`);
const buf = Buffer.from(await res.arrayBuffer());
if (buf.length > 8 * 1024 * 1024) throw new Error(`asset too big: ${url}`);
fs.writeFileSync(dest, buf);
}
for (const { id, url } of sketchUrls) {
let code;
try {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
code = await res.text();
} catch (ex) {
console.error(`${id}: source unreachable (${ex.message}) — keeping previous manifest`);
continue;
}
manifest[id] ??= {};
for (const [, , , ref] of code.matchAll(/(initImage\()\s*(["'])(.*?)\2/g)) {
if (manifest[id][ref]) continue;
// normalize: file:// work paths, absolute work paths, and git URLs
// (raw and plain) all map to a repo-relative path
const rel = ref
.replace(/^file:\/\/\/home\/pln\/Work\/Hydra\//, '')
.replace(/^\/home\/pln\/Work\/Hydra\//, '')
.replace(/^https?:\/\/[^/]+\/pln\/Hydra(?:\/raw\/[^/]+)?\//, '');
const destName = `${id}-${path.basename(rel).replace(/[^\w.-]/g, '_')}`;
const dest = path.join(OUT, destName);
const local = `/images/hydra/${destName}`;
try {
if (!fs.existsSync(dest)) {
await download(RAW + rel, dest);
fetched += 1;
}
manifest[id][ref] = local;
console.log(`${id}: ${rel} → ${local}`);
} catch (ex) {
failed += 1;
console.error(`${id}: asset failed (${ex.message})`);
}
}
}
fs.writeFileSync(MANIFEST, JSON.stringify(manifest, null, 1) + '\n');
console.log(`\n${fetched} asset(s) fetched, ${failed} failed — manifest: ${path.relative(process.cwd(), MANIFEST)}`);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment