Skip to content

Instantly share code, notes, and snippets.

@marcwieland95
Created April 10, 2024 09:40
Show Gist options
  • Save marcwieland95/1255f9c099128af9a7b3380b99a0aea4 to your computer and use it in GitHub Desktop.
Save marcwieland95/1255f9c099128af9a7b3380b99a0aea4 to your computer and use it in GitHub Desktop.
Prefetch sites on idle and prerender sites on hover
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