Catalog / Turbo.js Cheatsheet
Turbo.js Cheatsheet
A comprehensive guide to Turbo.js, covering its core concepts, components, and usage patterns for building modern web applications with enhanced speed and responsiveness.
Turbo Drive Fundamentals
Navigation & Page Updates
Turbo Drive: Automatically intercepts clicks on all Instead, Turbo Drive fetches the new page in the background and updates the current page’s |
Turbolinks-classic Compatibility: Turbo is designed as a successor to Turbolinks. Many concepts remain similar, but Turbo offers significant improvements, including more robust handling of JavaScript and asset loading. |
No Configuration Needed: To enable Turbo Drive, simply include the |
Meta Tags: You can control Turbo Drive’s behavior using meta tags in the Example: |
Page Visit Events
|
Fired before Turbo Drive starts a visit. |
|
Fired when Turbo Drive is about to fetch a new page. |
|
Fired before Turbo Drive caches the current page. |
|
Fired before Turbo Drive renders the new page. |
|
Fired after Turbo Drive renders the new page. |
|
Fired after Turbo Drive completes a visit and the new page is visible. |
Disabling Turbo Drive
You can disable Turbo Drive on specific links or forms by adding the Example: |
To disable Turbo Drive completely, remove the |
Turbo Frames
Encapsulating Page Sections
Turbo Frames: Allow you to update specific parts of a page without reloading the entire page. This is achieved by wrapping sections of your HTML in |
Lazy Loading: Turbo Frames can also be used for lazy loading content. Content within a frame is only loaded when the frame is scrolled into view (or when explicitly triggered). |
Frame Attributes
|
A unique identifier for the frame. Required for Turbo to target and update the frame. |
|
The URL to load the frame’s content from. The content fetched from this URL will replace the frame’s current content. |
|
Specifies the |
Basic Frame Example
|
In a Rails-like backend, a corresponding |
Frame Events
|
Fired after a Turbo Frame has loaded its content. |
|
Fired after a Turbo Frame has rendered the content |
Turbo Streams
Asynchronous DOM Updates
Turbo Streams: Deliver asynchronous DOM updates over WebSocket connections or server-sent events. Streams are particularly useful for real-time applications or scenarios where server-side events need to be reflected in the client-side UI immediately. |
Stream Actions: Turbo Streams use actions like |
Stream Message Format
Turbo Stream messages are typically sent as HTML fragments containing Example:
|
The |
Stream Actions
|
Appends the content to the end of the target element. |
|
Prepends the content to the beginning of the target element. |
|
Replaces the entire target element with the content. |
|
Replaces the content within the target element with the content. |
|
Removes the target element from the DOM. |
Advanced Turbo Techniques
Using `data-turbo-stream`
You can trigger Turbo Stream updates directly from links and forms using the Example:
|
Redirects and Turbo
When handling form submissions with Turbo, you can return a redirect response. Turbo Drive will automatically follow the redirect and update the page. If you need to perform additional actions after the redirect, you can use the |
JavaScript Considerations
Since Turbo Drive prevents full page loads, you need to ensure that your JavaScript code is compatible with Turbo. Use event delegation to attach event listeners to elements that may be replaced during Turbo Drive updates. Example:
|
Caching
Turbo Drive caches pages to improve performance. You can control caching behavior using meta tags and server-side headers. Use |