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
8 changes: 8 additions & 0 deletions src/utils/normalize-model-cache-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const normalizeModelCacheUrl = (url: string): string => {
return url
.replace(/([?&])cachebust_origin=[^&#]*(&)?/g, (_match, separator, hasMore) =>
separator === "?" && hasMore ? "?" : hasMore ? "&" : "",
Comment on lines +3 to +4
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Strip repeated cachebust_origin params completely

The new normalizer only removes the first cachebust_origin when the parameter appears multiple times, because the regex optionally consumes the following & and the next occurrence is no longer preceded by ? or &. For example, ...?cachebust_origin=a&cachebust_origin=b&pn=C1 normalizes to a URL that still contains one cachebust_origin, so equivalent cache-busted URLs can still produce different cache keys and duplicate OBJ cache entries.

Useful? React with 👍 / 👎.

)
.replace(/\?&/, "?")
.replace(/[?&]$/, "")
}
26 changes: 26 additions & 0 deletions tests/normalize-model-cache-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, test } from "bun:test"
import { normalizeModelCacheUrl } from "../src/utils/normalize-model-cache-url"

test("removes cachebust_origin when it is the only query param", () => {
expect(
normalizeModelCacheUrl(
"https://cdn.example.com/model.obj?cachebust_origin=abc123",
),
).toBe("https://cdn.example.com/model.obj")
})

test("removes cachebust_origin while preserving other query params", () => {
expect(
normalizeModelCacheUrl(
"https://cdn.example.com/model.obj?uuid=1&cachebust_origin=abc123&pn=C123",
),
).toBe("https://cdn.example.com/model.obj?uuid=1&pn=C123")
})

test("removes trailing blank cachebust_origin param", () => {
expect(
normalizeModelCacheUrl(
"https://cdn.example.com/model.obj?uuid=1&pn=C123&cachebust_origin=",
),
).toBe("https://cdn.example.com/model.obj?uuid=1&pn=C123")
})
Loading