Skip to content

Instantly share code, notes, and snippets.

@pazguille
Last active August 14, 2023 23:33
Show Gist options
  • Save pazguille/5eee6a4e5eb35129938db178b6bd3d45 to your computer and use it in GitHub Desktop.
Save pazguille/5eee6a4e5eb35129938db178b6bd3d45 to your computer and use it in GitHub Desktop.

Revisions

  1. pazguille revised this gist Aug 14, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion prefetch.js
    Original 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 prefetch = hasPrerender() ? prerender : hasPrefetch() ? viaPrefetch : viaFetch;

    const io = new IntersectionObserver(
    async (entries) => {
  2. pazguille revised this gist Aug 14, 2023. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion prefetch.js
    Original 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 = hasPrefetch() ? viaPrefetch : viaFetch;
    const prefetch = hasPrerender ? prerender : hasPrefetch() ? viaPrefetch : viaFetch;

    const io = new IntersectionObserver(
    async (entries) => {
  3. pazguille revised this gist Aug 14, 2023. 1 changed file with 32 additions and 1 deletion.
    33 changes: 32 additions & 1 deletion prefetch.js
    Original 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(() => {
    document.head.insertAdjacentHTML('beforeend', '<link rel="prefetch" href="'+ entry.target.href +'" />');
    prefetch(entry.target.href);
    io.unobserve(entry.target);
    });
    }
    });
    }
    );

    Array.from(document.querySelectorAll('a'))
    .forEach(link => io.observe(link));
    });
  4. pazguille created this gist Aug 27, 2022.
    27 changes: 27 additions & 0 deletions prefetch.js
    Original 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));
    });