Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fetchers = {};
- function prefetchCleanup(event) {
- clearTimeout(fetchers[event.target.href]);
- event.target.removeEventListener('mouseleave', prefetchCleanup);
- }
- document.addEventListener('mouseover', event => {
- let url = event.target.href;
- clearTimeout(fetchers[url]);
- if (!event.target.dataset.prefetch) return;
- if (Turbolinks.controller.cache.has(url)) return;
- event.target.addEventListener('mouseleave', prefetchCleanup);
- fetchers[url] = setTimeout(() => {
- const iframe = document.createElement('iframe');
- iframe.setAttribute('style', 'display:none');
- iframe.setAttribute('src', url);
- iframe.addEventListener('load', () => {
- Turbolinks.controller.cache.put(
- url,
- Turbolinks.Snapshot.fromElement(iframe.contentDocument.documentElement)
- );
- iframe.parentElement.removeChild(iframe);
- });
- document.body.appendChild(iframe);
- }, 600);
- });
Add Comment
Please, Sign In to add comment