Turbo 8 InstantClick (Turbo-Prefetch) Feature

What it Does

Turbo 8 introduces the InstantClick (also known as turbo-prefetch) feature, which significantly improves the perceived speed of your web application by preloading links before the user clicks on them. This feature predicts which…


This content originally appeared on DEV Community and was authored by Jess Alejo

What it Does

Turbo 8 introduces the InstantClick (also known as turbo-prefetch) feature, which significantly improves the perceived speed of your web application by preloading links before the user clicks on them. This feature predicts which links the user is likely to click on and preloads their content in the background. When the user actually clicks on the link, the content is loaded instantly, resulting in a faster and smoother user experience.

Demo:

  1. Without InstantClick:

    • User clicks on a link.
    • Browser sends a request to the server.
    • Server processes the request and responds with the new page.
    • Browser renders the new page.
  2. With InstantClick:

    • User hovers over a link.
    • Browser prefetches the page in the background.
    • User clicks on the link.
    • Prefetched page is displayed almost instantly.

Why Should I Use InstantClick?

  1. Enhanced User Experience: Faster page transitions lead to a smoother and more responsive user experience.

  2. Reduced Load Time: By prefetching pages, the perceived load time is reduced, making your application feel faster.

  3. Improved Engagement: Users are more likely to stay on your site and navigate through multiple pages when the experience is seamless.

  4. Competitive Advantage: Faster navigation can give you a competitive edge, as users tend to prefer websites that load quickly and efficiently.

How to Use InstantClick

The Turbo-prefetch feature was enabled by default starting from version 1.4.0 of the turbo-rails gem. This version includes the InstantClick feature, which automatically prefetches links to improve the perceived speed of web applications.

To take advantage of this feature, ensure that you have Turbo set up in your Rails application.

Add Turbo to Your Application: If you haven't already, add Turbo to your Gemfile:

gem 'turbo-rails'

Install Turbo: Run the following command to install Turbo:

bundle install
rails turbo:install

Enable InstantClick: InstantClick is enabled by default so you don't need to do anything extra. Your links will automatically use the prefetch feature.

How to Disable InstantClick

If you need to disable InstantClick for any reason, you can do so by modifying the Turbo configuration.

  1. Disable Globally: To disable the InstantClick feature globally without disabling the entire Turbo functionality, you can add a meta tag in your application layout.
<!DOCTYPE html>
<html>
<head>
  <meta name="turbo-prefetch" content="false">
  <!-- Other head elements -->
</head>
<body>
  <!-- Body content -->
</body>
</html>
  1. Disable for Specific Links: To disable InstantClick for specific links, add the data-turbo-prefetch attribute to the link tag.
<%= link_to "My Link", my_path, data: { turbo_prefetch: false } %>

Conclusion

Turbo 8's InstantClick feature is a powerful tool to enhance the performance and user experience of your web application. By preloading links, it significantly reduces the perceived load time, making your application feel faster and more responsive. However, you also have the flexibility to disable this feature globally or on specific links as needed.

Incorporating turbo-prefetch effectively can lead to higher user engagement, better SEO, and an overall smoother experience for your users.


This content originally appeared on DEV Community and was authored by Jess Alejo


Print Share Comment Cite Upload Translate Updates
APA

Jess Alejo | Sciencx (2024-07-01T01:12:58+00:00) Turbo 8 InstantClick (Turbo-Prefetch) Feature. Retrieved from https://www.scien.cx/2024/07/01/turbo-8-instantclick-turbo-prefetch-feature/

MLA
" » Turbo 8 InstantClick (Turbo-Prefetch) Feature." Jess Alejo | Sciencx - Monday July 1, 2024, https://www.scien.cx/2024/07/01/turbo-8-instantclick-turbo-prefetch-feature/
HARVARD
Jess Alejo | Sciencx Monday July 1, 2024 » Turbo 8 InstantClick (Turbo-Prefetch) Feature., viewed ,<https://www.scien.cx/2024/07/01/turbo-8-instantclick-turbo-prefetch-feature/>
VANCOUVER
Jess Alejo | Sciencx - » Turbo 8 InstantClick (Turbo-Prefetch) Feature. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/01/turbo-8-instantclick-turbo-prefetch-feature/
CHICAGO
" » Turbo 8 InstantClick (Turbo-Prefetch) Feature." Jess Alejo | Sciencx - Accessed . https://www.scien.cx/2024/07/01/turbo-8-instantclick-turbo-prefetch-feature/
IEEE
" » Turbo 8 InstantClick (Turbo-Prefetch) Feature." Jess Alejo | Sciencx [Online]. Available: https://www.scien.cx/2024/07/01/turbo-8-instantclick-turbo-prefetch-feature/. [Accessed: ]
rf:citation
» Turbo 8 InstantClick (Turbo-Prefetch) Feature | Jess Alejo | Sciencx | https://www.scien.cx/2024/07/01/turbo-8-instantclick-turbo-prefetch-feature/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.