instantpage / instant.page

Make your site’s pages instant in 1 minute and improve your conversion rate by 1%
https://instant.page
MIT License
6.04k stars 205 forks source link

Various changes #36

Closed andershagbard closed 5 years ago

andershagbard commented 5 years ago

This pull request consist of 3 commits.

Use strict equality comparison

Change comparison operators to strict.

Prevent preload if href starts with hash

Prevent links like <a href="#top">Go to top</a> preloading.

Check for 'noInstant' in dataset earlier in evaluation

Move check for noInstant in dataset up within the isPreloadable function. Makes more sense, and saves from some evaluation.

dieulot commented 5 years ago

I deem strict equality unnecessary. I’ve never been bitten by the lack of it.

Links starting with a hash on the same page are already prevented from being preloaded:

  if (preloadLocation.hash && preloadLocation.pathname + preloadLocation.search == location.pathname + location.search) {
    return
  }

The check for ‘noInstant’ is at the bottom because it’s not triggered as often as the other conditions.

andershagbard commented 5 years ago

Links starting with a hash on the same page are already prevented from being preloaded:

I've have just tested in Chrome, but should be working in similar ways for other browsers.

If the link doesn't contain a URL, it will preload. because the pathnames will not be equal.

var preloadLocation = new URL('#top')
console.log(preloadLocation.pathname) // => blank
console.log(location.pathname) // => Current path
dieulot commented 5 years ago

instant.page uses new URL(linkElement.href), .href gives an absolute URL (unlike .getAttribute('href')) even if it’s a relative URL in the HTML.