Skip to content

Commit

Permalink
fix: don't infinitely set the src which triggers the mutation observe…
Browse files Browse the repository at this point in the history
…r again (#287)
  • Loading branch information
mlohstroh authored Dec 5, 2024
1 parent 9ff4e82 commit 7adc57e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/utils/patchUrlMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,20 @@ 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') {
// Scripts are a special case, and need to be wholly recreated since
// 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);
}
}
}
}
Expand Down

0 comments on commit 7adc57e

Please sign in to comment.