From 7adc57e9aa020d8beac8839eb4da948d115fe40b Mon Sep 17 00:00:00 2001 From: Mark Lohstroh Date: Thu, 5 Dec 2024 11:49:22 -0600 Subject: [PATCH] fix: don't infinitely set the src which triggers the mutation observer again (#287) --- src/utils/patchUrlMappings.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/utils/patchUrlMappings.ts b/src/utils/patchUrlMappings.ts index e8e4c0a..4ed0e84 100644 --- a/src/utils/patchUrlMappings.ts +++ b/src/utils/patchUrlMappings.ts @@ -127,7 +127,8 @@ function recursivelyRemapChildNodes(node: Node, mappings: Mapping[]) { function attemptSetNodeSrc(node: Node, mappings: Mapping[]) { if (node instanceof HTMLElement && node.hasAttribute('src')) { - const url = absoluteURL(node.getAttribute('src') ?? ''); + const rawSrc = node.getAttribute('src'); + const url = absoluteURL(rawSrc ?? ''); if (url.host === window.location.host) return; if (node.tagName.toLowerCase() === 'script') { @@ -135,7 +136,11 @@ function attemptSetNodeSrc(node: Node, mappings: Mapping[]) { // modifying a script tag doesn't refetch. attemptRecreateScriptNode(node, {url, mappings}); } else { - node.setAttribute('src', attemptRemap({url, mappings}).toString()); + const newSrc = attemptRemap({url, mappings}).toString(); + // Only apply the remapping if we actually remapped the value + if (newSrc !== rawSrc) { + node.setAttribute('src', newSrc); + } } } }