From ca11162d027a0d3e3b5a65a54825b00d5689fb80 Mon Sep 17 00:00:00 2001 From: Shayan Ali Date: Mon, 12 Oct 2026 01:51:16 +1100 Subject: [PATCH] perf(browser): parse captured module URLs once per lookup (#1829) Co-authored-by: Shayan Ali <217162076+shayanxcyber@users.noreply.github.com> --- src/domain/webModuleImports.test.ts | 16 ++++++++++++++++ src/domain/webModuleImports.ts | 25 +++++++++++-------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/domain/webModuleImports.test.ts b/src/domain/webModuleImports.test.ts index 346938a4e..26db977a1 100644 --- a/src/domain/webModuleImports.test.ts +++ b/src/domain/webModuleImports.test.ts @@ -142,4 +142,20 @@ describe("captured module candidates", () => { ), ).toEqual([]); }); + it("retains exact non-URL identities without matching response fragments", () => { + const scripts = [ + script("relative/module.js#part", "page-script"), + script("relative/module.js#part", "scenario-response"), + script("relative/module.js", "scenario-response"), + ]; + expect( + matchCapturedModules("relative/module.js#part", scripts).map((item) => [ + item.script_index, + item.match, + ]), + ).toEqual([ + [0, "exact-reported-url"], + [1, "exact-reported-url"], + ]); + }); }); diff --git a/src/domain/webModuleImports.ts b/src/domain/webModuleImports.ts index fdcd36e83..026868955 100644 --- a/src/domain/webModuleImports.ts +++ b/src/domain/webModuleImports.ts @@ -108,6 +108,8 @@ export const matchCapturedModules = ( url: string, scripts: readonly ExportedWebScript[], ) => { + const transport = URL.parse(url); + if (transport !== null) transport.hash = ""; const candidates: { script_index: number; match: "exact-reported-url" | "response-url-without-fragment"; @@ -116,20 +118,15 @@ export const matchCapturedModules = ( scripts.forEach((script, script_index) => { if (script.url === url) candidates.push({ script_index, match: "exact-reported-url", script }); - else if (script.source.kind === "scenario-response") { - try { - const transport = new URL(url); - transport.hash = ""; - if (transport.href === script.url) - candidates.push({ - script_index, - match: "response-url-without-fragment", - script, - }); - } catch (cause: unknown) { - void cause; - } - } + else if ( + script.source.kind === "scenario-response" && + transport?.href === script.url + ) + candidates.push({ + script_index, + match: "response-url-without-fragment", + script, + }); }); return candidates; };