Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/hooks/use-global-obj-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useEffect } from "react"
import type { Object3D } from "three"
import { MTLLoader, OBJLoader } from "three-stdlib"
import { loadVrml } from "src/utils/vrml"
import { normalizeModelCacheUrl } from "src/utils/normalize-model-cache-url"

// Define the type for our cache
interface CacheItem {
Expand All @@ -28,7 +29,7 @@ export function useGlobalObjLoader(
useEffect(() => {
if (!url) return

const cleanUrl = url.replace(/&cachebust_origin=$/, "")
const cleanUrl = normalizeModelCacheUrl(url)

const cache = window.TSCIRCUIT_OBJ_LOADER_CACHE
let hasUrlChanged = false
Expand Down
18 changes: 18 additions & 0 deletions src/utils/normalize-model-cache-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const CACHE_BUST_PARAMS = new Set(["cachebust_origin", "cachebust"])

export function normalizeModelCacheUrl(url: string) {
try {
const parsedUrl = new URL(url, globalThis.location?.href ?? "http://localhost")

for (const param of CACHE_BUST_PARAMS) {
parsedUrl.searchParams.delete(param)
}

const normalized = parsedUrl.toString()
return parsedUrl.origin === "http://localhost"
? `${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`
: normalized
} catch {
return url.replace(/([?&])cachebust(?:_origin)?=[^&]*&?/g, "$1").replace(/[?&]$/, "")
}
}
22 changes: 22 additions & 0 deletions tests/normalize-model-cache-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test, expect } from "bun:test"
import { normalizeModelCacheUrl } from "../src/utils/normalize-model-cache-url"

test("removes cache bust params from model URLs", () => {
expect(
normalizeModelCacheUrl(
"https://modelcdn.tscircuit.com/model.obj?uuid=abc&cachebust_origin=123",
),
).toBe("https://modelcdn.tscircuit.com/model.obj?uuid=abc")

expect(
normalizeModelCacheUrl(
"https://modelcdn.tscircuit.com/model.obj?cachebust=456&uuid=abc",
),
).toBe("https://modelcdn.tscircuit.com/model.obj?uuid=abc")
})

test("keeps relative model URLs relative", () => {
expect(normalizeModelCacheUrl("/models/part.obj?cachebust_origin=123")).toBe(
"/models/part.obj",
)
})