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
33 changes: 24 additions & 9 deletions src/hooks/use-global-obj-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ if (typeof window !== "undefined" && !window.TSCIRCUIT_OBJ_LOADER_CACHE) {
window.TSCIRCUIT_OBJ_LOADER_CACHE = new Map<string, CacheItem>()
}

export function getObjLoaderCacheKey(url: string): string {
try {
const base =
typeof window !== "undefined" ? window.location.href : undefined
const parsedUrl = new URL(url, base)
parsedUrl.searchParams.delete("cachebust_origin")
return parsedUrl.href
} catch {
return url
.replace(/([?&])cachebust_origin=[^&#]*/g, "$1")
.replace(/[?&]$/, "")
}
}

export function useGlobalObjLoader(
url: string | null,
): Object3D | null | Error {
Expand All @@ -28,21 +42,22 @@ export function useGlobalObjLoader(
useEffect(() => {
if (!url) return

const cleanUrl = url.replace(/&cachebust_origin=$/, "")
const fetchUrl = url.replace(/&cachebust_origin=$/, "")
const cacheKey = getObjLoaderCacheKey(fetchUrl)

const cache = window.TSCIRCUIT_OBJ_LOADER_CACHE
let hasUrlChanged = false

async function loadAndParseObj() {
try {
if (cleanUrl.endsWith(".wrl")) {
return await loadVrml(cleanUrl)
if (fetchUrl.endsWith(".wrl")) {
return await loadVrml(fetchUrl)
}

const response = await fetch(cleanUrl)
const response = await fetch(fetchUrl)
if (!response.ok) {
throw new Error(
`Failed to fetch "${cleanUrl}": ${response.status} ${response.statusText}`,
`Failed to fetch "${fetchUrl}": ${response.status} ${response.statusText}`,
)
}
const text = await response.text()
Expand Down Expand Up @@ -79,8 +94,8 @@ export function useGlobalObjLoader(
}

function loadUrl() {
if (cache.has(cleanUrl)) {
const cacheItem = cache.get(cleanUrl)!
if (cache.has(cacheKey)) {
const cacheItem = cache.get(cacheKey)!
if (cacheItem.result) {
// If we have a result, clone it
return Promise.resolve(cacheItem.result.clone())
Expand All @@ -97,10 +112,10 @@ export function useGlobalObjLoader(
// If the result is an Error, return it
return result
}
cache.set(cleanUrl, { ...cache.get(cleanUrl)!, result })
cache.set(cacheKey, { ...cache.get(cacheKey)!, result })
return result
})
cache.set(cleanUrl, { promise, result: null })
cache.set(cacheKey, { promise, result: null })
return promise
}

Expand Down
32 changes: 32 additions & 0 deletions tests/use-global-obj-loader-cache-key.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, test } from "bun:test"
import { getObjLoaderCacheKey } from "../src/hooks/use-global-obj-loader"

test("removes cachebust_origin from the OBJ loader cache key", () => {
expect(
getObjLoaderCacheKey(
"https://modelcdn.tscircuit.com/easyeda_models/download?uuid=model-1&pn=C123&cachebust_origin=http%3A%2F%2Flocalhost%3A6006",
),
).toBe(
"https://modelcdn.tscircuit.com/easyeda_models/download?uuid=model-1&pn=C123",
)
})

test("keeps non-cachebust query parameters in the OBJ loader cache key", () => {
expect(
getObjLoaderCacheKey(
"https://modelcdn.tscircuit.com/easyeda_models/download?cachebust_origin=https%3A%2F%2Fexample.com&uuid=model-1&pn=C123",
),
).toBe(
"https://modelcdn.tscircuit.com/easyeda_models/download?uuid=model-1&pn=C123",
)
})

test("keeps URLs unchanged when no cachebust_origin parameter is present", () => {
expect(
getObjLoaderCacheKey(
"https://modelcdn.tscircuit.com/easyeda_models/download?uuid=model-1&pn=C123",
),
).toBe(
"https://modelcdn.tscircuit.com/easyeda_models/download?uuid=model-1&pn=C123",
)
})
Loading