1.0.4 • Published 3 years ago

turbolinks-prefetch v1.0.4

Weekly downloads
390
License
MIT
Repository
github
Last release
3 years ago

Turbolinks Prefetch

Turbolinks extend for prefetch links to speeds up your website.

中文介绍与说明

WorkFlow

hover --> [prefetch] --<no cache>--> [XHR fetch] -> [Turbolinks cache.put]
              |
          <exist cache / in fetching>
              |
            ignore

click --<check cache>-- exist --> [isPrefetch] -> [Turbolinks.visit advance] ---> [render page]
             |                         |                 |
             |                         |                 --async-> [fetch background] -> [render if updated]
             |                         |
             |                       <Yes>
             |                         |--- [Turbolinks.visit restore] --> render -> nothing
          No cahce
             |
             ---> [Turbolinks.visit]

Installation

$ yarn add turbolinks-prefetch

Usage

import Turbolinks from 'turbolinks';
window.Turbolinks = Turbolinks;

import TurbolinksPrefetch from 'turbolinks-prefetch';
TurbolinksPrefetch.start();

Prefetch request will send Purpose: prefetch header, so you may need this to ignore some thing.

For example views count increment:

class TopicsController < ApplicationController
  def show
    if request.headers["Purpose"] != "prefetch"
      # Ignore hit counter for prefetch request
      @topic.increment_hit
    end
  end
end

Disable prefetch

By default, TurbolinksPrefetch will enable prefetch for all links.

Except:

  • Links not have same host (Origin);
  • Open in new window links target="_blank";
  • The links has data-remote attribute;
  • The links has data-method attribute;
  • The links has data-prefetch="false" attribute;

If you wants disable prefetch, you can do like this:

<a href="https://google.com">Google</>
<a href="/topics/123" target="_blank">Open in new window</a>
<a href="/topics/123" data-method="PUT" data-remote>Put</a>
<a href="/topics/123" data-method="DELETE">Delete</a>
<a href="/topics/123" data-prefetch="false">Disable by directly</a>

Demo

https://github.com/ruby-china/homeland/commit/e1378468703b8c3cfd7e33a17dc703ff8294a3e9

Demo site

1.0.4

3 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago