From 44c4b9d3d98acff071e4a98bd3650fd478d938fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=A3=E5=85=83=E5=BE=81?= Date: Tue, 12 May 2026 21:29:06 +0800 Subject: [PATCH] Normalize cachebust_origin when building model loader cache keys --- src/hooks/use-global-obj-loader.ts | 3 ++- src/utils/normalize-model-cache-url.ts | 8 ++++++++ tests/normalize-model-cache-url.test.ts | 26 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/utils/normalize-model-cache-url.ts create mode 100644 tests/normalize-model-cache-url.test.ts diff --git a/src/hooks/use-global-obj-loader.ts b/src/hooks/use-global-obj-loader.ts index 69422095..ba23742a 100644 --- a/src/hooks/use-global-obj-loader.ts +++ b/src/hooks/use-global-obj-loader.ts @@ -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 { @@ -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 diff --git a/src/utils/normalize-model-cache-url.ts b/src/utils/normalize-model-cache-url.ts new file mode 100644 index 00000000..7f305080 --- /dev/null +++ b/src/utils/normalize-model-cache-url.ts @@ -0,0 +1,8 @@ +export const normalizeModelCacheUrl = (url: string): string => { + return url + .replace(/([?&])cachebust_origin=[^&#]*(&)?/g, (_match, separator, hasMore) => + separator === "?" && hasMore ? "?" : hasMore ? "&" : "", + ) + .replace(/\?&/, "?") + .replace(/[?&]$/, "") +} diff --git a/tests/normalize-model-cache-url.test.ts b/tests/normalize-model-cache-url.test.ts new file mode 100644 index 00000000..acbec684 --- /dev/null +++ b/tests/normalize-model-cache-url.test.ts @@ -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") +})