Last active
August 14, 2023 23:33
-
-
Save pazguille/5eee6a4e5eb35129938db178b6bd3d45 to your computer and use it in GitHub Desktop.
Revisions
-
pazguille revised this gist
Aug 14, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -42,7 +42,7 @@ window.addEventListener('load', () => { document.head.appendChild(s); } const prefetch = hasPrerender() ? prerender : hasPrefetch() ? viaPrefetch : viaFetch; const io = new IntersectionObserver( async (entries) => { -
pazguille revised this gist
Aug 14, 2023 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -31,14 +31,18 @@ window.addEventListener('load', () => { xhr.send(); } function hasPrerender() { return HTMLScriptElement.supports && HTMLScriptElement.supports('speculationrules'); } function prerender(url) { const s = document.createElement('script'); s.type = 'speculationrules'; s.text = '{"prerender":[{"source": "list","urls": ["'+ url +'"]}]}'; document.head.appendChild(s); } const prefetch = hasPrerender ? prerender : hasPrefetch() ? viaPrefetch : viaFetch; const io = new IntersectionObserver( async (entries) => { -
pazguille revised this gist
Aug 14, 2023 . 1 changed file with 32 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,18 +10,49 @@ window.addEventListener('load', () => { }); }, 1); }; function hasPrefetch() { const link = document.createElement('link'); return link.relList && link.relList.supports && link.relList.supports('prefetch'); } function viaPrefetch(url) { document.head.insertAdjacentHTML('beforeend', '<link rel="prefetch" href="'+ url +'" />'); } function viaFetch(url) { fetch(url, {credentials: 'include'}); } function viaXHR(url) { const xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.open('GET', url, true); xhr.send(); } function prerender(url) { const s = document.createElement('script'); s.type = 'speculationrules'; s.text = '{"prerender":[{"source": "list","urls": ["'+ url +'"]}]}'; document.head.appendChild(s); } const prefetch = hasPrefetch() ? viaPrefetch : viaFetch; const io = new IntersectionObserver( async (entries) => { entries.forEach(function (entry) { if (entry.isIntersecting) { requestIdleCallback(() => { prefetch(entry.target.href); io.unobserve(entry.target); }); } }); } ); Array.from(document.querySelectorAll('a')) .forEach(link => io.observe(link)); }); -
pazguille created this gist
Aug 27, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ window.addEventListener('load', () => { window.requestIdleCallback = window.requestIdleCallback || function (cb) { const start = Date.now(); return setTimeout(function () { cb({ didTimeout: false, timeRemaining: function () { return Math.max(0, 50 - (Date.now() - start)); } }); }, 1); }; const io = new IntersectionObserver( async (entries) => { entries.forEach(function (entry) { if (entry.isIntersecting) { requestIdleCallback(() => { document.head.insertAdjacentHTML('beforeend', '<link rel="prefetch" href="'+ entry.target.href +'" />'); io.unobserve(entry.target); }); } }); } ); Array.from(document.querySelectorAll('a')) .forEach(link => io.observe(link)); });