Skip to content

Commit

Permalink
Only swap fragments if they don't have an identical `data-fragment-ha…
Browse files Browse the repository at this point in the history
…sh` attribute
  • Loading branch information
hirasso committed Jun 12, 2023
1 parent 59c0a61 commit 644e794
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/inc/FragmentPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type Direction = 'forwards' | 'backwards';
export type Route = {
from: string;
to: string;
}
};

type RuleOptions = {
from: Path;
Expand Down Expand Up @@ -53,7 +53,8 @@ export default class FragmentPlugin extends Plugin {
};

this.rules = this.options.rules.map(
({ from, to, direction, fragments, name }) => new Rule(from, to, direction, fragments, name)
({ from, to, direction, fragments, name }) =>
new Rule(from, to, direction, fragments, name)
);
}

Expand Down Expand Up @@ -100,10 +101,10 @@ export default class FragmentPlugin extends Plugin {
/**
* Set the current fragment when clicking a link
*/
onClickLink: Handler<"clickLink"> = (event) => {
onClickLink: Handler<'clickLink'> = (event) => {
this.selectedRule = this.findSelectedRule({
from: this.swup.getCurrentUrl(),
to: Location.fromElement((event.delegateTarget as HTMLAnchorElement)).url
to: Location.fromElement(event.delegateTarget as HTMLAnchorElement).url
});
};

Expand Down Expand Up @@ -195,12 +196,23 @@ export default class FragmentPlugin extends Plugin {
console.warn('[swup] Container missing in current document:', selector);
return;
}

// Bail early if the two fragments have identical attribute values 'data-fragment-hash'
if (this.isSameFragmentHash(currentElement, incomingElement)) return;

currentElement.replaceWith(incomingElement);
});
}
/**
* Check if two elements have an identical attribute `data-fragment-hash`
*/
isSameFragmentHash(el1: Element, el2: Element) {
const hash1 = el1.getAttribute('data-fragment-hash');
const hash2 = el2.getAttribute('data-fragment-hash');
return hash1 && hash2 && hash1 === hash2;
}
}


/**
* - Allow one-directional routes
* - Ignore fragments where [data-fragment-hash] is the same
Expand Down

0 comments on commit 644e794

Please sign in to comment.