Created
April 10, 2024 09:40
-
-
Save marcwieland95/1255f9c099128af9a7b3380b99a0aea4 to your computer and use it in GitHub Desktop.
Prefetch sites on idle and prerender sites on hover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { listen } from 'quicklink'; | |
import ProbaClick from 'probaclick'; | |
// Quicklink | |
window.addEventListener('load', () => { | |
listen({ | |
ignores: [(uri) => uri.includes('wp-') || uri.includes('.pdf') || uri.includes('.zip')], | |
}); | |
}); | |
// Prerender | |
ProbaClick('a', { | |
delay: 300, | |
callback: function (link) { | |
maybeMakeHint({ | |
link, | |
type: 'prerender', | |
}); | |
}, | |
}); | |
let hasBeen = { | |
prefetch: [], | |
prerender: [], | |
}; | |
export const makeHint = (href, type) => { | |
let link = document.createElement('link'); | |
link.setAttribute('rel', type); | |
link.setAttribute('href', href); | |
document.head.appendChild(link); | |
return href; | |
}; | |
const isExternalLink = (href) => { | |
return !href.match(/^\//) && !href.includes(window.location.host); | |
}; | |
const maybeMakeHint = ({ link, type } = {}) => { | |
let href = link.getAttribute('href').split('?')[0]; | |
if (isExternalLink(href)) return; | |
if (hasBeen[type].includes(href)) return; | |
hasBeen[type].push(makeHint(href, type)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment