Turbolinks makes navigating your web application faster
Turbolinks makes navigating your web application faster
Please note that Turbolinks is no longer under active development. It has been superseded by a new framework called Turbo, which is part of the Hotwire umbrella.
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 <body>, and merges its <head>, all without incurring the cost of a full page load.
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 <script> 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 start() function.
Your Ruby on Rails application can use the turbolinks RubyGem to install Turbolinks. This gem contains a Rails engine which integrates seamlessly with the Rails asset pipeline.
turbolinks gem, version 5, to your Gemfile: gem 'turbolinks', '~> 5.2.0'bundle install.//= require turbolinks to your JavaScript manifest file (usually found at app/assets/javascripts/application.js).The gem also provides server-side support for Turbolinks redirection, which can be used without the asset pipeline.
Your application can use the turbolinks npm package to install Turbolinks as a module for build tools like webpack.
Add the turbolinks 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.
Building Your Turbolinks Application
Turbolinks intercepts all clicks on <a href> 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 XMLHttpRequest, and then renders the HTML response.
During rendering, Turbolinks replaces the current <body> element outright and merges the contents of the <head> element. The JavaScript window and document objects, and the HTML <html> element, persist from one rendering to the next.
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 are initiated by clicking a Turbolinks-enabled link, or programmatically by calling Turbolinks.visit(location).
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.
The default visit action is advance. During an advance visit, Turbolinks pushes a new entry onto the browser’s history stack using history.pushState.
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.
You may wish to visit a location without pushing a new history entry onto the stack. The replace visit action uses history.replaceState 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 data-turbolinks-action="replace":
<a href="/edit" data-turbolinks-action="replace">Edit</a>To programmatically visit a location with the replace action, pass the action: "replace" option to Turbolinks.visit:
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.
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.
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 Turbolinks.visit with an action of restore.
Application visits can be canceled before they start, regardless of whether they were initiated by a link click or a call to Turbolinks.visit.
Listen for the turbolinks:before-visit event to be notified when a visit is about to start, and use event.data.url (or $event.originalEvent.data.url, when using jQuery) to check the visit’s location. Then cancel the visit by calling event.preventDefault().
Restoration visits cannot be canceled and do not fire turbolinks:before-visit. Turbolinks issues restoration visits in response to history navigation that has already taken place, typically via the browser’s Back or Forward buttons.
Turbolinks can be disabled on a per-link basis by annotating a link or any of its ancestors with data-turbolinks="false".
<a href="/" data-turbolinks="false">Disabled</a>
To reenable when an ancestor has opted out, use data-turbolinks="true":
Links with Turbolinks disabled will be handled normally by the browser.
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 window and document 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.
Your browser automatically loads and evaluates any <script> elements present on the initial page load.
When you navigate to a new page, Turbolinks looks for any <script> elements in the new page’s <head> which aren’t present on the current page. Then it appends them to the current <head> where they’re loaded and evaluated by the browser. You can use this to load additional JavaScript files on-demand.
Turbolinks evaluates `<s
reload data-turbolinks-permanent section sometimes
CSP nonce make reload on every access with data-turbolinks-track
IE 11 issue - Loading specific script tag in head section in Turbolinks
Refused to execute inline script because it violates the following Content Security Policy
Full page reload triggered on path ending in .
Redirect url with anchor not working.
Turbolinks fails with target = "_self"
Select box selection is not restored when using back button (works in turbolinks-classic)
async/defer
error Uncaught DOMException: Failed to execute 'setAttribute' on 'Element': '{$nonce}' is not a valid attribute name