Commit 4f9a5cda by PLN (Algolia)

hydra: asset rewrite runs longest-ref-first (relative key was a substring of the…

hydra: asset rewrite runs longest-ref-first (relative key was a substring of the absolute git URL for yet — short-first mangled it into a CORS-rejecting hybrid), one shared helper for both call sites
parent 403fb68c
...@@ -4,17 +4,15 @@ import SyntaxHighlighter from "react-syntax-highlighter"; ...@@ -4,17 +4,15 @@ import SyntaxHighlighter from "react-syntax-highlighter";
import Layout from "@/components/layout"; import Layout from "@/components/layout";
import utilStyles from "@/styles/utils.module.css"; import utilStyles from "@/styles/utils.module.css";
import { getAllHydraIds, getHydraData } from "@/lib/hydras"; import { getAllHydraIds, getHydraData } from "@/lib/hydras";
import { withLocalAssets } from "@/lib/hydra-sketch";
import HydraClient from "./HydraClient"; import HydraClient from "./HydraClient";
/** Same rewrite table as the 404 embed (see scripts/hydra-assets.mjs): /** Same rewrite table as the 404 embed (see scripts/hydra-assets.mjs):
* cross-origin initImage refs (NASA, unsplash…) fail as WebGL textures, * cross-origin initImage refs (NASA, unsplash…) fail as WebGL textures,
* so they are pointed at the local copies before the source runs. */ * so they are pointed at the local copies before the source runs. */
function withLocalAssets(source, id) { function rewritesFor(id) {
const manifestPath = path.join(process.cwd(), "public/images/hydra/manifest.json"); const manifestPath = path.join(process.cwd(), "public/images/hydra/manifest.json");
const rewrites = JSON.parse(fs.readFileSync(manifestPath, "utf8"))[id] ?? {}; return JSON.parse(fs.readFileSync(manifestPath, "utf8"))[id] ?? {};
let out = source;
for (const [ref, local] of Object.entries(rewrites)) out = out.split(ref).join(local);
return out;
} }
export function generateStaticParams() { export function generateStaticParams() {
...@@ -32,7 +30,7 @@ export default async function Hydra({ params }) { ...@@ -32,7 +30,7 @@ export default async function Hydra({ params }) {
const hydraData = await getHydraData(id); const hydraData = await getHydraData(id);
const response = await fetch(hydraData.source); const response = await fetch(hydraData.source);
hydraData.source = withLocalAssets(await response.text(), id); hydraData.source = withLocalAssets(await response.text(), rewritesFor(id));
return ( return (
<Layout> <Layout>
......
...@@ -153,3 +153,18 @@ export async function resolveHydraSketch(url) { ...@@ -153,3 +153,18 @@ export async function resolveHydraSketch(url) {
memo.set(url, work); memo.set(url, work);
return work; return work;
} }
/** Rewrite a sketch's initImage asset refs to their local copies
* (public/images/hydra — table built by scripts/hydra-assets.mjs).
* Longest ref first: the same file can ship both a relative key and its
* absolute git URL, and the relative is a substring of the absolute —
* short-first replacement mangles the absolute into a hybrid
* (`…/raw/master//images/hydra/x.jpg`) that CORS-rejects at runtime,
* leaving the sketch silently black (yet, 2026-11-07). */
export function withLocalAssets(source, rewrites) {
let out = source;
for (const [ref, local] of Object.entries(rewrites).sort((a, b) => b[0].length - a[0].length)) {
out = out.split(ref).join(local);
}
return out;
}
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