Commit 03ba3ad4 by PLN (Algolia)

hydra pages: rewrite initImage refs to local assets (cross-origin textures fail…

hydra pages: rewrite initImage refs to local assets (cross-origin textures fail in WebGL — lucia21 was blank)
parent 77b0fc29
import fs from "fs";
import path from "path";
import SyntaxHighlighter from "react-syntax-highlighter"; 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 HydraClient from "./HydraClient"; import HydraClient from "./HydraClient";
/** Same rewrite table as the 404 embed (see scripts/hydra-assets.mjs):
* cross-origin initImage refs (NASA, unsplash…) fail as WebGL textures,
* so they are pointed at the local copies before the source runs. */
function withLocalAssets(source, id) {
const manifestPath = path.join(process.cwd(), "public/images/hydra/manifest.json");
const rewrites = 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() {
return getAllHydraIds().map((id) => ({ id })); return getAllHydraIds().map((id) => ({ id }));
} }
...@@ -19,7 +32,7 @@ export default async function Hydra({ params }) { ...@@ -19,7 +32,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 = await response.text(); hydraData.source = withLocalAssets(await response.text(), id);
return ( return (
<Layout> <Layout>
......
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