Skip to content

fix: normalize filled cachebust_origin values in model cache key (#93)#915

Open
leninug wants to merge 1 commit into
tscircuit:mainfrom
leninug:fix/93-normalize-cachebust-origin-with-value
Open

fix: normalize filled cachebust_origin values in model cache key (#93)#915
leninug wants to merge 1 commit into
tscircuit:mainfrom
leninug:fix/93-normalize-cachebust-origin-with-value

Conversation

@leninug
Copy link
Copy Markdown

@leninug leninug commented May 21, 2026

Summary

Fixes a narrow but real duplicate-load case in useGlobalObjLoader that #93 was opened for: URLs that differ only in their cachebust_origin query parameter currently produce different cache keys, so the same OBJ is fetched, parsed, and cloned twice.

The existing strip only matched the trailing empty form &cachebust_origin=. Real production fixtures send the filled form (&cachebust_origin=https%3A%2F%2Ftscircuit.com), which slipped through unchanged. Two boards that reference the same underlying model from different origins hit the cache miss path on every render.

Evidence in the repo (no changes to fixtures, just demonstrating the gap):

  • stories/assets/left-flashlight-board.json — uses the empty form (handled by old regex)
  • stories/assets/complex-board.json, nine-key-keyboard.json — use the filled form (not handled by old regex)

Change

  1. New pure utility src/utils/get-3d-model-cache-key.ts — strips cachebust_origin regardless of position (first / middle / last / only-param), with or without a value, and preserves the URL fragment. No dependency on URL (which would throw on relative inputs).

  2. useGlobalObjLoader swaps the inline regex for the utility — one-line wire-up, no behavior change for URLs that don't carry cachebust_origin.

  3. New tests/get-3d-model-cache-key.test.ts — 13 unit tests covering:

    • Empty and filled trailing values
    • First / middle / only-param positions
    • URLs with a #fragment
    • Lookalike prefixes (cachebust_origin_extra) — must NOT be stripped
    • Value-less cachebust_origin (no =)
    • Two URLs that differ only in cachebust_origin → same key
    • A real production URL from complex-board.json

Test results

bun test tests/get-3d-model-cache-key.test.ts
 13 pass
 0 fail
 13 expect() calls
Ran 13 tests across 1 file. [29.00ms]

Full suite shows the same 5 pre-existing failures before and after this change (all in preprocess-circuit-json / SVG snapshot tests, unrelated to the cache layer).

Why this is scoped narrowly

The repo already has several recently-merged PRs propagating cache layers to other loaders (GLTF/STL/STEP/WRL). This PR intentionally only touches the OBJ loader path and extracts the normalization into a reusable utility. The utility is exported so other loaders can adopt it incrementally without duplicating the logic.

Algora

/claim #93

Extract URL cache-key normalization into a pure `get3DModelCacheKey`
utility and route `useGlobalObjLoader` through it.

Before this change the cache-busting strip only matched the trailing
empty form `&cachebust_origin=`. Real production fixtures (see
`stories/assets/complex-board.json`, `nine-key-keyboard.json`) send the
filled form `&cachebust_origin=https%3A%2F%2Ftscircuit.com`, which
slipped through unchanged. Two boards referencing the same underlying
model from different origins produced different cache keys, so the
same OBJ was fetched, parsed, and cloned twice — the exact duplicate-
load pattern described in tscircuit#93.

The new utility handles every position of the parameter (first / middle
/ last / only-param), with or without a value, and preserves the URL
fragment. Wire-up in `useGlobalObjLoader` is a one-line swap.

13 unit tests cover empty / filled / first / middle / trailing /
hash-bearing / lookalike-prefix cases plus a real production fixture
URL, and assert that two URLs differing only in `cachebust_origin`
collapse to the same key.

All existing tests continue to pass.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
3d-viewer Ready Ready Preview, Comment May 21, 2026 8:40am

Request Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant