Skip to content

Instantly share code, notes, and snippets.

@Ch4s3
Last active February 9, 2022 17:26
Show Gist options
  • Select an option

  • Save Ch4s3/49a018d32e93bbf7d52064ad9b314d78 to your computer and use it in GitHub Desktop.

Select an option

Save Ch4s3/49a018d32e93bbf7d52064ad9b314d78 to your computer and use it in GitHub Desktop.

Revisions

  1. Ch4s3 revised this gist Oct 24, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion turboLinksPreFetch.js
    Original file line number Diff line number Diff line change
    @@ -75,7 +75,7 @@ const isNotGetMethod = function(link) {
    link.attributes['data-method'] && (link.attributes['data-method'].value !== 'get')
    }

    //This function returns true if the link or location shouldn't be preloaded
    //This function returns true if the link or location shouldn't be
    const notPreloadable = function(link, location){
    if (preloadAttribute(link) === false) {
    return true;
    @@ -104,6 +104,8 @@ const preload = function(event) {
    let cache = Turbolinks.controller.cache.get(location);
    if (!cache) {
    cache = Turbolinks.controller.cache.get(`prefetch${location}`);
    }
    if (!cache) {
    const request = new Turbolinks.CachedHttpRequest(null, location, window.location);
    Turbolinks.controller.cache.put(`prefetch${location}`, request);
    return(request.send());
  2. Ch4s3 revised this gist Oct 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion turboLinksPreFetch.js
    Original file line number Diff line number Diff line change
    @@ -75,7 +75,7 @@ const isNotGetMethod = function(link) {
    link.attributes['data-method'] && (link.attributes['data-method'].value !== 'get')
    }

    //This function returns true if the link or location shouldn't be
    //This function returns true if the link or location shouldn't be preloaded
    const notPreloadable = function(link, location){
    if (preloadAttribute(link) === false) {
    return true;
  3. Ch4s3 revised this gist Oct 24, 2017. 1 changed file with 34 additions and 49 deletions.
    83 changes: 34 additions & 49 deletions turboLinksPreFetch.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,23 @@
    /*
    * decaffeinate suggestions:
    * DS001: Remove Babel/TypeScript constructor workaround
    * DS102: Remove unnecessary code created because of implicit returns
    * DS103: Rewrite code to no longer use __guard__
    * DS207: Consider shorter variations of null checks
    * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
    */
    const OldHttpRequest = Turbolinks.HttpRequest;

    Turbolinks.CachedHttpRequest = class CachedHttpRequest extends Turbolinks.HttpRequest {
    constructor(_, location, referrer) {
    {
    // Hack: trick Babel/TypeScript into allowing this before super.
    if (false) { super(); }
    let thisFn = (() => { this; }).toString();
    let thisName = thisFn.slice(thisFn.indexOf('{') + 1, thisFn.indexOf(';')).trim();
    eval(`${thisName} = this;`);
    }
    super();
    super(this, location, referrer);
    }

    requestCompletedWithResponse(response, redirectedToLocation) {
    this.response = response;
    return this.redirect = redirectedToLocation;
    return(this.redirect = redirectedToLocation);
    }

    requestFailedWithStatusCode(code) {
    return this.failCode = code;
    return(this.failCode = code);
    }

    oldSend() {
    @@ -35,17 +26,17 @@ Turbolinks.CachedHttpRequest = class CachedHttpRequest extends Turbolinks.HttpRe
    this.setProgress(0);
    this.xhr.send();
    this.sent = true;
    return __guardMethod__(this.delegate, 'requestStarted', o => o.requestStarted());
    return(this.delegate, 'requestStarted', o => o.requestStarted());
    }
    }

    send() {
    if (this.failCode) {
    return this.delegate.requestFailedWithStatusCode(this.failCode, this.failText);
    return(this.delegate.requestFailedWithStatusCode(this.failCode, this.failText));
    } else if (this.response) {
    return this.delegate.requestCompletedWithResponse(this.response, this.redirect);
    return(this.delegate.requestCompletedWithResponse(this.response, this.redirect));
    } else {
    return this.oldSend();
    return(this.oldSend());
    }
    }
    };
    @@ -61,72 +52,66 @@ Turbolinks.HttpRequest = class HttpRequest {
    cache.delegate = delegate;
    return cache;
    } else {
    return new OldHttpRequest(delegate, location, referrer);
    return(new OldHttpRequest(delegate, location, referrer));
    }
    }
    };

    Turbolinks.SnapshotCache.prototype.delete = function(location) {
    const key = Turbolinks.Location.wrap(location).toCacheKey();
    return delete this.snapshots[key];
    return(delete this.snapshots[key]);
    };

    const preloadAttribute = function(link) {
    const attr = link.attributes['data-turbolinks-preload']
    if (attr == null || attr.value === 'false') {
    const linkAttr = link.attributes['data-turbolinks-preload']
    if (!linkAttr || linkAttr.value === 'false') {
    return false;
    } else {
    return true;
    }
    }

    const isNotGetMethod = function(link) {
    (link.attributes['data-method'] != null) && (method.value !== 'get')
    link.attributes['data-method'] && (link.attributes['data-method'].value !== 'get')
    }

    //This function returns true if the link or location shouldn't be
    const notPreloadable = function(link, location){
    if (preloadAttribute(link) === false) {
    return true;
    } else if (isNotGetMethod(link)) {
    return true;
    } else if (location.anchor || location.absoluteURL.endsWith("#")) {
    return true;
    } else if (location.absoluteURL === window.location.href) {
    return true;
    } else {
    return false;
    }
    }

    const preload = function(event) {
    let link;
    if (link = Turbolinks.controller.getVisitableLinkForNode(event.target)) {
    let location;
    if (location = Turbolinks.controller.getVisitableLocationForLink(link)) {
    let link = Turbolinks.controller.getVisitableLinkForNode(event.target);
    if (link) {
    let location = Turbolinks.controller.getVisitableLocationForLink(link);
    if (location) {
    if (Turbolinks.controller.applicationAllowsFollowingLinkToLocation(link, location)) {
    let method;
    if (preloadAttribute(link) === false) {
    return;
    }
    if (isNotGetMethod(link)) {
    return;
    }
    if (location.anchor || location.absoluteURL.endsWith("#")) {
    if (notPreloadable(link, location)) {
    return;
    }
    if (location.absoluteURL === window.location.href) {
    return;
    }

    // If Turbolinks has already cached this location internally, use that default behavior
    // otherwise we can try and prefetch it here
    let cache = Turbolinks.controller.cache.get(location);
    if (!cache) {
    cache = Turbolinks.controller.cache.get(`prefetch${location}`);
    }

    if (!cache) {
    const request = new Turbolinks.CachedHttpRequest(null, location, window.location);
    Turbolinks.controller.cache.put(`prefetch${location}`, request);
    return request.send();
    return(request.send());
    }
    }
    }
    }
    };

    document.addEventListener("touchstart", preload);
    document.addEventListener("mouseover", preload);
    function __guardMethod__(obj, methodName, transform) {
    if (typeof obj !== 'undefined' && obj !== null && typeof obj[methodName] === 'function') {
    return transform(obj, methodName);
    } else {
    return undefined;
    }
    }
    document.addEventListener("mouseover", preload);
  4. Ch4s3 created this gist Oct 24, 2017.
    132 changes: 132 additions & 0 deletions turboLinksPreFetch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    /*
    * decaffeinate suggestions:
    * DS001: Remove Babel/TypeScript constructor workaround
    * DS102: Remove unnecessary code created because of implicit returns
    * DS103: Rewrite code to no longer use __guard__
    * DS207: Consider shorter variations of null checks
    * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
    */
    const OldHttpRequest = Turbolinks.HttpRequest;

    Turbolinks.CachedHttpRequest = class CachedHttpRequest extends Turbolinks.HttpRequest {
    constructor(_, location, referrer) {
    {
    // Hack: trick Babel/TypeScript into allowing this before super.
    if (false) { super(); }
    let thisFn = (() => { this; }).toString();
    let thisName = thisFn.slice(thisFn.indexOf('{') + 1, thisFn.indexOf(';')).trim();
    eval(`${thisName} = this;`);
    }
    super(this, location, referrer);
    }

    requestCompletedWithResponse(response, redirectedToLocation) {
    this.response = response;
    return this.redirect = redirectedToLocation;
    }

    requestFailedWithStatusCode(code) {
    return this.failCode = code;
    }

    oldSend() {
    if (this.xhr && !this.sent) {
    this.notifyApplicationBeforeRequestStart();
    this.setProgress(0);
    this.xhr.send();
    this.sent = true;
    return __guardMethod__(this.delegate, 'requestStarted', o => o.requestStarted());
    }
    }

    send() {
    if (this.failCode) {
    return this.delegate.requestFailedWithStatusCode(this.failCode, this.failText);
    } else if (this.response) {
    return this.delegate.requestCompletedWithResponse(this.response, this.redirect);
    } else {
    return this.oldSend();
    }
    }
    };


    Turbolinks.HttpRequest = class HttpRequest {
    constructor(delegate, location, referrer) {
    const cache = Turbolinks.controller.cache.get(`prefetch${location}`);
    if (cache) {
    //Turbolinks.controller.cache = new Turbolinks.SnapshotCache 10
    Turbolinks.controller.cache.delete(`prefetch${location}`);
    console.log(JSON.stringify(Turbolinks.controller.cache.keys));
    cache.delegate = delegate;
    return cache;
    } else {
    return new OldHttpRequest(delegate, location, referrer);
    }
    }
    };

    Turbolinks.SnapshotCache.prototype.delete = function(location) {
    const key = Turbolinks.Location.wrap(location).toCacheKey();
    return delete this.snapshots[key];
    };

    const preloadAttribute = function(link) {
    const attr = link.attributes['data-turbolinks-preload']
    if (attr == null || attr.value === 'false') {
    return false;
    } else {
    return true;
    }
    }

    const isNotGetMethod = function(link) {
    (link.attributes['data-method'] != null) && (method.value !== 'get')
    }

    const preload = function(event) {
    let link;
    if (link = Turbolinks.controller.getVisitableLinkForNode(event.target)) {
    let location;
    if (location = Turbolinks.controller.getVisitableLocationForLink(link)) {
    if (Turbolinks.controller.applicationAllowsFollowingLinkToLocation(link, location)) {
    let method;
    if (preloadAttribute(link) === false) {
    return;
    }
    if (isNotGetMethod(link)) {
    return;
    }
    if (location.anchor || location.absoluteURL.endsWith("#")) {
    return;
    }
    if (location.absoluteURL === window.location.href) {
    return;
    }

    // If Turbolinks has already cached this location internally, use that default behavior
    // otherwise we can try and prefetch it here
    let cache = Turbolinks.controller.cache.get(location);
    if (!cache) {
    cache = Turbolinks.controller.cache.get(`prefetch${location}`);
    }

    if (!cache) {
    const request = new Turbolinks.CachedHttpRequest(null, location, window.location);
    Turbolinks.controller.cache.put(`prefetch${location}`, request);
    return request.send();
    }
    }
    }
    }
    };

    document.addEventListener("touchstart", preload);
    document.addEventListener("mouseover", preload);
    function __guardMethod__(obj, methodName, transform) {
    if (typeof obj !== 'undefined' && obj !== null && typeof obj[methodName] === 'function') {
    return transform(obj, methodName);
    } else {
    return undefined;
    }
    }