Skip to content

Commit 470f802

Browse files
fix(catalog): resolve assets a script loads by name (#3170)
The 3D blocks fetch their model with loader.load("models/iphone.glb"), which no attribute or url() pattern can see, so the path survived into the payload untouched. Inside a srcdoc iframe it resolved against the docs page and 404'd, leaving vfx-iphone-device and ios26-liquid-glass mounted and playing with an empty scene. Names that resolve to a real file in the item's own directory now count as references. They are deliberately weaker than markup references: a name that is not a file is ignored rather than failing the item, so an ordinary string that happens to look like a filename cannot push an item onto the video fallback. Also clears an item's payload when a run decides it cannot build one, so the page generator stops finding a previous run's file on disk and emitting a player for a preview that no longer exists.
1 parent 536165b commit 470f802

6 files changed

Lines changed: 99 additions & 7 deletions

File tree

‎docs/public/catalog/blocks/ios26-liquid-glass.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎docs/public/catalog/blocks/macos-tahoe-liquid-glass.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎docs/public/catalog/blocks/vfx-iphone-device.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎scripts/catalog-payload-assets.test.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { existsSync, mkdirSync, mkdtempSync, readdirSync, writeFileSync } from "
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { describe, it } from "node:test";
6-
import { externalizeDataUris, localReferences, processAssets } from "./catalog-payload-assets.ts";
6+
import {
7+
externalizeDataUris,
8+
localReferences,
9+
probableReferences,
10+
processAssets,
11+
} from "./catalog-payload-assets.ts";
712

813
function project(files: Record<string, string | Buffer>): string {
914
const dir = mkdtempSync(join(tmpdir(), "hf-payload-assets-"));
@@ -188,3 +193,51 @@ describe("externalizeDataUris", () => {
188193
assert.match(result.html, /data:model\/gltf-binary/);
189194
});
190195
});
196+
197+
describe("probableReferences", () => {
198+
it("finds a model a script loads by name", () => {
199+
// `loader.load("models/iphone.glb")` is how the 3D blocks fetch their
200+
// model, and no attribute or url() pattern can see it.
201+
const html = `<script>loader.load("models/iphone.glb", cb);</script>`;
202+
assert.deepEqual(probableReferences(html), ["models/iphone.glb"]);
203+
});
204+
205+
it("does not repeat a reference an attribute already covers", () => {
206+
const html = `<img src="a.png"><script>preload("a.png")</script>`;
207+
assert.deepEqual(probableReferences(html), []);
208+
});
209+
});
210+
211+
describe("processAssets with script-loaded files", () => {
212+
it("inlines a model loaded by name, since the host will not publish glb", () => {
213+
const dir = project({ "models/iphone.glb": Buffer.alloc(64, 3) });
214+
const result = processAssets(
215+
`<script>loader.load("models/iphone.glb")</script>`,
216+
dir,
217+
target(),
218+
);
219+
220+
assert.equal(result.inlined, 1);
221+
assert.match(result.html, /data:model\/gltf-binary;base64,/);
222+
assert.deepEqual(result.unresolved, []);
223+
});
224+
225+
it("ignores a name that is not a file, rather than failing the item", () => {
226+
// An ordinary string that happens to look like a filename must not push an
227+
// item onto the video fallback.
228+
const result = processAssets(
229+
`<script>track("checkout.step.png")</script>`,
230+
project({}),
231+
target(),
232+
);
233+
234+
assert.deepEqual(result.unresolved, []);
235+
assert.equal(result.hosted + result.inlined, 0);
236+
});
237+
238+
it("still fails an item whose markup points at a missing file", () => {
239+
const result = processAssets(`<img src="gone.png">`, project({}), target());
240+
241+
assert.deepEqual(result.unresolved, ["gone.png"]);
242+
});
243+
});

‎scripts/catalog-payload-assets.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ export function localReferences(html: string): string[] {
9595
return [...found];
9696
}
9797

98+
/**
99+
* Files a script loads by name, such as `loader.load("models/iphone.glb")`.
100+
*
101+
* These cannot be told apart from ordinary strings by shape alone, so unlike
102+
* the definite references above they are only acted on when the name resolves
103+
* to a real file in the item's own directory, and a miss is ignored rather than
104+
* failing the item. Without this pass a 3D model stayed a relative path, which
105+
* resolves against the docs page inside a `srcdoc` iframe and 404s: the preview
106+
* renders, just with nothing in it.
107+
*/
108+
export function probableReferences(html: string): string[] {
109+
const definite = new Set(localReferences(html));
110+
const found = new Set<string>();
111+
for (const [, ref] of html.matchAll(/["']([^"'\s]+\.[a-z0-9]{2,5})["']/gi)) {
112+
if (!ref || definite.has(ref)) continue;
113+
if (/^(https?:|data:|blob:|mailto:|#|%23|\/\/)/i.test(ref)) continue;
114+
if (!MIME_TYPES[extname(pathPart(ref)).toLowerCase()]) continue;
115+
found.add(ref);
116+
}
117+
return [...found];
118+
}
119+
98120
export interface AssetResult {
99121
html: string;
100122
/** Written once to the shared directory and linked. */
@@ -131,14 +153,22 @@ export function processAssets(html: string, projectDir: string, target: AssetTar
131153
let inlined = 0;
132154
const unresolved: string[] = [];
133155

134-
for (const ref of localReferences(html)) {
156+
const definite = localReferences(html);
157+
const candidates = [
158+
...definite.map((ref) => ({ ref, strict: true })),
159+
...probableReferences(html).map((ref) => ({ ref, strict: false })),
160+
];
161+
162+
for (const { ref, strict } of candidates) {
135163
const source = resolve(projectDir, pathPart(ref));
136164

137165
// A composition reaching outside its own directory would pull an arbitrary
138166
// file from the build machine into a published payload.
139167
const contained = source === root || source.startsWith(`${root}/`);
140168
if (!contained || !existsSync(source) || !statSync(source).isFile()) {
141-
unresolved.push(ref);
169+
// A name a script passed around that turned out not to be a file is just
170+
// a string; only a reference we are sure about counts as a broken one.
171+
if (strict) unresolved.push(ref);
142172
continue;
143173
}
144174

‎scripts/generate-catalog-payloads.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ function typeDir(kind: ItemKind): string {
5252
}
5353

5454
async function buildPayload(item: CatalogItem): Promise<"written" | "skipped"> {
55+
const outPath = join(payloadRoot, typeDir(item.kind), `${item.name}.json`);
56+
57+
// An item that stops qualifying has to lose its payload, or the page
58+
// generator keeps finding one on disk and emits a player for a preview this
59+
// run just decided it cannot build.
60+
const dropStalePayload = () => rmSync(outPath, { force: true });
61+
5562
const projectDir = await prepareProjectDir(item);
5663
try {
5764
const html = readFileSync(join(projectDir, "index.html"), "utf-8");
@@ -71,15 +78,16 @@ async function buildPayload(item: CatalogItem): Promise<"written" | "skipped"> {
7178
// keeps the MP4 rather than shipping a preview with holes in it.
7279
if (unresolved.length > 0) {
7380
console.log(` – ${item.name}: cannot inline ${unresolved.slice(0, 3).join(", ")}`);
81+
dropStalePayload();
7482
return "skipped";
7583
}
7684
const bytes = Buffer.byteLength(withShared, "utf-8");
7785
if (bytes > MAX_PAYLOAD_BYTES) {
7886
console.log(` – ${item.name}: ${(bytes / 1e6).toFixed(1)} MB payload, over budget`);
87+
dropStalePayload();
7988
return "skipped";
8089
}
8190

82-
const outPath = join(payloadRoot, typeDir(item.kind), `${item.name}.json`);
8391
mkdirSync(dirname(outPath), { recursive: true });
8492
writeFileSync(outPath, JSON.stringify({ html: withShared }), "utf-8");
8593

@@ -124,6 +132,7 @@ async function main(): Promise<void> {
124132
else skipped += 1;
125133
} catch (err) {
126134
failed += 1;
135+
rmSync(join(payloadRoot, typeDir(item.kind), `${item.name}.json`), { force: true });
127136
console.error(` ✗ ${item.name}: ${err instanceof Error ? err.message : err}`);
128137
}
129138
}

0 commit comments

Comments
 (0)