Github url


by turbolinks

turbolinks /turbolinks

Turbolinks makes navigating your web application faster

11.2K Stars 578 Forks Last release: almost 2 years ago (v5.2.0) MIT License 356 Commits 14 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Turbolinks® makes navigating your web application faster. Get the performance benefits of a single-page application without the added complexity of a client-side JavaScript framework. Use HTML to render your views on the server side and link to pages as usual. When you follow a link, Turbolinks automatically fetches the page, swaps in its

, and merges its

, all without incurring the cost of a full page load.



  • Optimizes navigation automatically. No need to annotate links or specify which parts of the page should change.
  • No server-side cooperation necessary. Respond with full HTML pages, not partial page fragments or JSON.
  • Respects the web. The Back and Reload buttons work just as you’d expect. Search engine-friendly by design.
  • Supports mobile apps. Adapters for iOS and Android let you build hybrid applications using native navigation controls.

Supported Browsers

Turbolinks works in all modern desktop and mobile browsers. It depends on the HTML5 History API and Window.requestAnimationFrame. In unsupported browsers, Turbolinks gracefully degrades to standard navigation.


Turbolinks automatically initializes itself when loaded via a standalone


tag or a traditional concatenated JavaScript bundle. If you load Turbolinks as a CommonJS or AMD module, first require the module, then call the provided



Installation Using Ruby on Rails

Your Ruby on Rails application can use the [


RubyGem]( to install Turbolinks. This gem contains a Rails engine which integrates seamlessly with the Rails asset pipeline.

  1. Add the
    gem, version 5, to your Gemfile:
    gem 'turbolinks', '~\> 5.2.0'
  2. Run
    bundle install
  3. Add
    //= require turbolinks
    to your JavaScript manifest file (usually found at

The gem also provides server-side support for Turbolinks redirection, which can be used without the asset pipeline.

Installation Using npm

Your application can use the [


npm package]( to install Turbolinks as a module for build tools like webpack.

  1. Add the
    package to your application:
    npm install --save turbolinks

Require and start Turbolinks in your JavaScript bundle:

var Turbolinks = require("turbolinks") Turbolinks.start()

The npm package alone does not provide server-side support for Turbolinks redirection. See Following Redirects for details on adding support.

Table of Contents

Navigating with Turbolinks- Each Navigation is a Visit- Application Visits- Restoration Visits- Canceling Visits Before They Start- Disabling Turbolinks on Specific Links

Building Your Turbolinks Application- Working with Script Elements - Loading Your Application’s JavaScript Bundle- Understanding Caching - Preparing the Page to be Cached - Detecting When a Preview is Visible - Opting Out of Caching- Installing JavaScript Behavior - Observing Navigation Events - Attaching Behavior With Stimulus- Making Transformations Idempotent- Persisting Elements Across Page Loads

Advanced Usage- Displaying Progress- Reloading When Assets Change- Ensuring Specific Pages Trigger a Full Reload- Setting a Root Location- Following Redirects- Redirecting After a Form Submission- Setting Custom HTTP Headers

API Reference- Turbolinks.visit- Turbolinks.clearCache- Turbolinks.setProgressBarDelay- Turbolinks.supported- Full List of Events

Contributing to Turbolinks- Building From Source- Running Tests

Navigating with Turbolinks

Turbolinks intercepts all clicks on

links to the same domain. When you click an eligible link, Turbolinks prevents the browser from following it. Instead, Turbolinks changes the browser’s URL using the History API, requests the new page using [


](, and then renders the HTML response.

During rendering, Turbolinks replaces the current

element outright and merges the contents of the

element. The JavaScript




objects, and the HTML

element, persist from one rendering to the next.

Each Navigation is a Visit

Turbolinks models navigation as a visit to a location (URL) with an action.

Visits represent the entire navigation lifecycle from click to render. That includes changing browser history, issuing the network request, restoring a copy of the page from cache, rendering the final response, and updating the scroll position.

There are two types of visit: an application visit, which has an action of advance or replace, and a restoration visit, which has an action of restore.

Application Visits

Application visits are initiated by clicking a Turbolinks-enabled link, or programmatically by calling [



An application visit always issues a network request. When the response arrives, Turbolinks renders its HTML and completes the visit.

If possible, Turbolinks will render a preview of the page from cache immediately after the visit starts. This improves the perceived speed of frequent navigation between the same pages.

If the visit’s location includes an anchor, Turbolinks will attempt to scroll to the anchored element. Otherwise, it will scroll to the top of the page.

Application visits result in a change to the browser’s history; the visit’s action determines how.

Advance visit action

The default visit action is advance. During an advance visit, Turbolinks pushes a new entry onto the browser’s history stack using [



Applications using the Turbolinks iOS adapter typically handle advance visits by pushing a new view controller onto the navigation stack. Similarly, applications using the Android adapter typically push a new activity onto the back stack.

Replace visit action

You may wish to visit a location without pushing a new history entry onto the stack. The replace visit action uses [


]( to discard the topmost history entry and replace it with the new location.

To specify that following a link should trigger a replace visit, annotate the link with




To programmatically visit a location with the replace action, pass the

action: "replace"

option to [



Turbolinks.visit("/edit", { action: "replace" })

Applications using the Turbolinks iOS adapter typically handle replace visits by dismissing the topmost view controller and pushing a new view controller onto the navigation stack without animation.

Restoration Visits

Turbolinks automatically initiates a restoration visit when you navigate with the browser’s Back or Forward buttons. Applications using the iOS or Android adapters initiate a restoration visit when moving backward in the navigation stack.

Restore visit action

If possible, Turbolinks will render a copy of the page from cache without making a request. Otherwise, it will retrieve a fresh copy of the page over the network. See Understanding Caching for more details.

Turbolinks saves the scroll position of each page before navigating away and automatically returns to this saved position on restoration visits.

Restoration visits have an action of restore and Turbolinks reserves them for internal use. You should not attempt to annotate links or invoke [


]( with an action of



Canceling Visits Before They Start

Application visits can be canceled before they start, regardless of whether they were initiated by a link click or a call to [



Listen for the


event to be notified when a visit is about to start, and use



, when using jQuery) to check the visit’s location. Then cancel the visit by calling



Restoration visits cannot be canceled and do not fire


. Turbolinks issues restoration visits in response to history navigation that has already taken place, typically via the browser’s Back or Forward buttons.

Disabling Turbolinks on Specific Links

Turbolinks can be disabled on a per-link basis by annotating a link or any of its ancestors with




To reenable when an ancestor has opted out, use




Links with Turbolinks disabled will be handled normally by the browser.

Building Your Turbolinks Application

Turbolinks is fast because it doesn’t reload the page when you follow a link. Instead, your application becomes a persistent, long-running process in the browser. This requires you to rethink the way you structure your JavaScript.

In particular, you can no longer depend on a full page load to reset your environment every time you navigate. The JavaScript




objects retain their state across page changes, and any other objects you leave in memory will stay in memory.

With awareness and a little extra care, you can design your application to gracefully handle this constraint without tightly coupling it to Turbolinks.

Working with Script Elements

Your browser automatically loads and evaluates any


elements present on the initial page load.

When you navigate to a new page, Turbolinks looks for any


elements in the new page’s

which aren’t present on the current page. Then it appends them to the current

where they’re loaded and evaluated by the browser. You can use this to load additional JavaScript files on-demand.

Turbolinks evaluates


elements in a page’s

each time it renders the page. You can use inline body scripts to set up per-page JavaScript state or bootstrap client-side models. To install behavior, or to perform more complex operations when the page changes, avoid script elements and use the


event instead.



elements with


if you do not want Turbolinks to evaluate them after rendering. Note that this annotation will not prevent your browser from evaluating scripts on the initial page load.

Loading Your Application’s JavaScript Bundle

Always make sure to load your application’s JavaScript bundle using


elements in the

of your document. Otherwise, Turbolinks will reload the bundle with every page change.

... <script src="/application-cbd3cd4.js" defer></script>

If you have traditionally placed application scripts at the end of

for performance reasons, consider using the [

<script defer></script>

]( attribute instead. It has widespread browser support and allows you to keep your scripts in

for Turbolinks compatibility.

You should also consider configuring your asset packaging system to fingerprint each script so it has a new URL when its contents change. Then you can use the


attribute to force a full page reload when you deploy a new JavaScript bundle. See Reloading When Assets Change for information.

Understanding Caching

Turbolinks maintains a cache of recently visited pages. This cache serves two purposes: to display pages without accessing the network during restoration visits, and to improve perceived performance by showing temporary previews during application visits.

When navigating by history (via Restoration Visits), Turbolinks will restore the page from cache without loading a fresh copy from the network, if possible.

Otherwise, during standard navigation (via Application Visits), Turbolinks will immediately restore the page from cache and display it as a preview while simultaneously loading a fresh copy from the network. This gives the illusion of instantaneous page loads for frequently accessed locations.

Turbolinks saves a copy of the current page to its cache just before rendering a new page. Note that Turbolinks copies the page using [


](, which means any attached event listeners and associated data are discarded.

Preparing the Page to be Cached

Listen for the


event if you need to prepare the document before Turbolinks caches it. You can use this event to reset forms, collapse expanded UI elements, or tear down any third-party widgets so the page is ready to be displayed again.

document.addEventListener("turbolinks:before-cache", function() { // ... })

Detecting When a Preview is Visible

Turbolinks adds a


attribute to the

element when it displays a preview from cache. You can check for the presence of this attribute to selectively enable or disable behavior when a preview is visible.

if (document.documentElement.hasAttribute("data-turbolinks-preview")) { // Turbolinks is displaying a preview }

Opting Out of Caching

You can control caching behavior on a per-page basis by including a

<meta name="turbolinks-cache-control">

element in your page’s

and declaring a caching directive.

Use the


directive to specify that a cached version of the page should not be shown as a preview during an application visit. Pages marked no-preview will only be used for restoration visits.

To specify that a page should not be cached at all, use the


directive. Pages marked no-cache will always be fetched over the network, including during restoration visits.

... <meta name="turbolinks-cache-control" content="no-cache">

To completely disable caching in your application, ensure every page contains a no-cache directive.

Installing JavaScript Behavior

You may be used to installing JavaScript behavior in response to the




, or jQuery


events. With Turbolinks, these events will fire only in response to the initial page load, not after any subsequent page changes. We compare two strategies for connecting JavaScript behavior to the DOM below.

Observing Navigation Events

Turbolinks triggers a series of events during navigation. The most significant of these is the


event, which fires once on the initial page load, and again after every Turbolinks visit.

You can observe the


event in place of


to set up JavaScript behavior after every page change:

document.addEventListener("turbolinks:load", function() { // ... })

Keep in mind that your application will not always be in a pristine state when this event is fired, and you may need to clean up behavior installed for the previous page.

Also note that Turbolinks navigation may not be the only source of page updates in your application, so you may wish to move your initialization code into a separate function which you can call from


and anywhere else you may change the DOM.

When possible, avoid using the


event to add other event listeners directly to elements on the page body. Instead, consider using event delegation to register event listeners once on





See the Full List of Events for more information.

Attaching Behavior With Stimulus

New DOM elements can appear on the page at any time by way of Ajax request handlers, WebSocket handlers, or client-side rendering operations, and these elements often need to be initialized as if they came from a fresh page load.

You can handle all of these updates, including updates from Turbolinks page loads, in a single place with the conventions and lifecycle callbacks provided by Turbolinks’ sister framework, Stimulus.

Stimulus lets you annotate your HTML with controller, action, and target attributes:

<input data-target="" type="text"><button data-action="click-&gt;hello#greet">Greet</button>

Implement a compatible controller and Stimulus connects it automatically:

// hello\_controller.js import { Controller } from "stimulus" export default class extends Controller { greet() { console.log(`Hello, ${}!`) } get name() { return this.targets.find("name").value } }

Stimulus connects and disconnects these controllers and their associated event handlers whenever the document changes using the MutationObserver API. As a result, it handles Turbolinks page changes the same way it handles any other type of DOM update.

See the Stimulus repository on GitHub for more information.

Making Transformations Idempotent

Often you’ll want to perform client-side transformations to HTML received from the server. For example, you might want to use the browser’s knowledge of the user’s current time zone to group a collection of elements by date.

Suppose you have annotated a set of elements with


attributes indicating the elements’ creation times in UTC. You have a JavaScript function that queries the document for all such elements, converts the timestamps to local time, and inserts date headers before each element that occurs on a new day.

Consider what happens if you’ve configured this function to run on


. When you navigate to the page, your function inserts date headers. Navigate away, and Turbolinks saves a copy of the transformed page to its cache. Now press the Back button—Turbolinks restores the page, fires


again, and your function inserts a second set of date headers.

To avoid this problem, make your transformation function idempotent. An idempotent transformation is safe to apply multiple times without changing the result beyond its initial application.

One technique for making a transformation idempotent is to keep track of whether you’ve already performed it by setting a


attribute on each processed element. When Turbolinks restores your page from cache, these attributes will still be present. Detect these attributes in your transformation function to determine which elements have already been processed.

A more robust technique is simply to detect the transformation itself. In the date grouping example above, that means checking for the presence of a date divider before inserting a new one. This approach gracefully handles newly inserted elements that weren’t processed by the original transformation.

Persisting Elements Across Page Loads

Turbolinks allows you to mark certain elements as permanent. Permanent elements persist across page loads, so that any changes you make to those elements do not need to be reapplied after navigation.

Consider a Turbolinks application with a shopping cart. At the top of each page is an icon with the number of items currently in the cart. This counter is updated dynamically with JavaScript as items are added and removed.

Now imagine a user who has navigated to several pages in this application. She adds an item to her cart, then presses the Back button in her browser. Upon navigation, Turbolinks restores the previous page’s state from cache, and the cart item count erroneously changes from 1 to 0.

You can avoid this problem by marking the counter element as permanent. Designate permanent elements by giving them an HTML


and annotating them with



1 item

Before each render, Turbolinks matches all permanent elements by


and transfers them from the original page to the new page, preserving their data and event listeners.

Advanced Usage

Displaying Progress

During Turbolinks navigation, the browser will not display its native progress indicator. Turbolinks installs a CSS-based progress bar to provide feedback while issuing a request.

The progress bar is enabled by default. It appears automatically for any page that takes longer than 500ms to load. (You can change this delay with the [


]( method.)

The progress bar is a

element with the class name 


. Its default styles appear first in the document and can be overridden by rules that come later.

For example, the following CSS will result in a thick green progress bar:

.turbolinks-progress-bar { height: 5px; background-color: green; }

To disable the progress bar entirely, set its


 style to 



.turbolinks-progress-bar { visibility: hidden; }

## Reloading When Assets Change

Turbolinks can track the URLs of asset elements in
 from one page to the next and automatically issue a full reload if they change. This ensures that users always have the latest versions of your application’s scripts and styles.

Annotate asset elements with


 and include a version identifier in your asset URLs. The identifier could be a number, a last-modified timestamp, or better, a digest of the asset’s contents, as in the following example.


## Ensuring Specific Pages Trigger a Full Reload

You can ensure visits to a certain page will always trigger a full reload by including a
``` element in the page’s ```


This setting may be useful as a workaround for third-party JavaScript libraries that don’t interact well with Turbolinks page changes.

## Setting a Root Location

By default, Turbolinks only loads URLs with the same origin—i.e. the same protocol, domain name, and port—as the current document. A visit to any other URL falls back to a full page load.

In some cases, you may want to further scope Turbolinks to a path on the same origin. For example, if your Turbolinks application lives at


, and the non-Turbolinks help site lives at 


, links from the app to the help site shouldn’t use Turbolinks.

Include a
``` element in your pages’ ```
 to scope Turbolinks to a particular root location. Turbolinks will only load same-origin URLs that are prefixed with this path.


## Following Redirects

When you visit location


 and the server redirects you to location 


, you expect the browser’s address bar to display the redirected URL.

However, Turbolinks makes requests using


, which transparently follows redirects. There’s no way for Turbolinks to tell whether a request resulted in a redirect without additional cooperation from the server.

To work around this problem, send the


 header in the final response to a visit that was redirected, and Turbolinks will replace the browser’s topmost history entry with the value you provide.

The Turbolinks Rails engine sets


 automatically when using 


 in response to a Turbolinks visit.
## Redirecting After a Form Submission

Submitting an HTML form to the server and redirecting in response is a common pattern in web applications. Standard form submission is similar to navigation, resulting in a full page load. Using Turbolinks you can improve the performance of form submission without complicating your server-side code.

Instead of submitting forms normally, submit them with XHR. In response to an XHR submit on the server, return JavaScript that performs a [


]( to be evaluated by the browser.

If form submission results in a state change on the server that affects cached pages, consider clearing Turbolinks’ cache with [



The Turbolinks Rails engine performs this optimization automatically for non-GET XHR requests that redirect with the


## Setting Custom HTTP Headers

You can observe the


 event to set custom headers on Turbolinks requests. Access the request’s XMLHttpRequest object via

, then call the [


]( method as many times as you wish.

For example, you might want to include a request ID with every Turbolinks link click and programmatic visit.

document.addEventListener("turbolinks:request-start", function(event) { var xhr = xhr.setRequestHeader("X-Request-Id", "123...") })

# API Reference

## Turbolinks.visit


js Turbolinks.visit(location) Turbolinks.visit(location, { action: action })

Performs an [Application Visit]( to the given _location_ (a string containing a URL or path) with the specified _action_ (a string, either





If _location_ is a cross-origin URL, or falls outside of the specified root (see [Setting a Root Location](, or if the value of [


]( is


, Turbolinks performs a full page load by setting 



If _action_ is unspecified, Turbolinks assumes a value of



Before performing the visit, Turbolinks fires a


 event on 


. Your application can listen for this event and cancel the visit with 


 (see [Canceling Visits Before They Start](
## Turbolinks.clearCache


js Turbolinks.clearCache()

Removes all entries from the Turbolinks page cache. Call this when state has changed on the server that may affect cached pages.

## Turbolinks.setProgressBarDelay


js Turbolinks.setProgressBarDelay(delayInMilliseconds)

Sets the delay after which the [progress bar]( will appear during navigation, in milliseconds. The progress bar appears after 500ms by default.

Note that this method has no effect when used with the iOS or Android adapters.

## Turbolinks.supported


js if (Turbolinks.supported) { // ... }

Detects whether Turbolinks is supported in the current browser (see [Supported Browsers](

## Full List of Events

Turbolinks emits events that allow you to track the navigation lifecycle and respond to page loading. Except where noted, Turbolinks fires events on the


Note that when using jQuery, the data on the event must be accessed as 




 fires when you click a Turbolinks-enabled link. The clicked element is the event target. Access the requested location with

. Cancel this event to let the click fall through to the browser as normal navigation.


 fires before visiting a location, except when navigating by history. Access the requested location with

. Cancel this event to prevent navigation.


 fires immediately after a visit starts.


 fires before Turbolinks issues a network request to fetch the page. Access the XMLHttpRequest object with



 fires after the network request completes. Access the XMLHttpRequest object with



 fires before Turbolinks saves the current page to cache.


 fires before rendering the page. Access the new 
 element with



 fires after Turbolinks renders the page. This event fires twice during an application visit to a cached location: once after rendering the cached version, and again after rendering the fresh version.


 fires once after the initial page load, and again after every Turbolinks visit. Access visit timing metrics with the


# Contributing to Turbolinks

Turbolinks is open-source software, freely distributable under the terms of an [MIT-style license]( The [source code is hosted on GitHub]( Development is sponsored by [Basecamp](

We welcome contributions in the form of bug reports, pull requests, or thoughtful discussions in the [GitHub issue tracker](

Please note that this project is released with a [Contributor Code of Conduct]( By participating in this project you agree to abide by its terms.

## Building From Source

Turbolinks is written in [TypeScript]( To build from source, first make sure you have the [Yarn package manager]( installed. Then issue the following commands to build the distributable files in



$ yarn install $ yarn build

Include the resulting


 file in your application’s JavaScript bundle.
## Running Tests

Turbolinks is tested with the [Intern testing library](

To run the test suite, follow the instructions for _Building From Source_ above, then run:

$ yarn test

If you are testing changes to the Turbolinks source, remember to run

yarn build

 before each test run. Or, run 

yarn watch

 to build automatically as you work.
* * *

© 2019 Basecamp, LLC.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.