Skip to content

Commit

Permalink
Prevent duplicate link rel prefetches and prefetching the current page (
Browse files Browse the repository at this point in the history
#54)

* Prevent duplicate link rel prefetches and prevent prefetching the current page.

Add two checks against the prospective href to prefetch:
1. Has the href already been prefetched?
2. Is the href equal to the current page?

There is not a benefit to duplicate prefetches or prefetching the current page.
If either check is true, return without prefetching the href.

* Remove unnecessary check for duplicate fetches as prefetch cache should already account for this.

* Include query string in the URL comparison (exclude the fragment).

Co-authored-by: nickFalcone <nfalcone@u.rochester.edu>
  • Loading branch information
nickFalcone and nickFalcone authored Nov 17, 2020
1 parent 8705578 commit 504a29f
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ function prefetch(e) {
if (e.target.origin != location.origin) {
return;
}
/**
* Return the given url with no fragment
* @param {string} url potentially containing a fragment
* @return {string} url without fragment
*/
const removeUrlFragment = (url) => url.split("#")[0];
if (removeUrlFragment(window.location.href) === removeUrlFragment(e.target.href)) {
return;
}
var l = document.createElement("link");
l.rel = "prefetch";
l.href = e.target.href;
Expand Down

0 comments on commit 504a29f

Please sign in to comment.