turbolinks-prefetch

1.0.4 • Public • Published

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

截屏2020-09-28 17 17 44

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

Demo site

Readme

Keywords

none

Package Sidebar

Install

npm i turbolinks-prefetch

Weekly Downloads

19

Version

1.0.4

License

MIT

Unpacked Size

7.31 kB

Total Files

6

Last publish

Collaborators

  • huacnlee