Skip to content

Instantly share code, notes, and snippets.

@spilth
Created March 10, 2022 00:23
Show Gist options
  • Save spilth/06f5303f4197024fe34a1d60031f96e6 to your computer and use it in GitHub Desktop.
Save spilth/06f5303f4197024fe34a1d60031f96e6 to your computer and use it in GitHub Desktop.
Rails 7 Turbo Drive Basics

Rails 7 Turbo Drive

This is a quick example to help illustrate what a Rails 7 application does when Turbo Drive is enabled versus when it is disabled.

It is assumed you are using the following version of Ruby and Rails:

$ ruby --version
ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-darwin21]
$ rails --version
Rails 7.0.2.3

Project Setup

First we spin up a simles Rails 7 app with some scaffolding:

$ rails new whatever
$ cd whatever
$ bin/setup
$ rails generate scaffold album title:string artist:string
$ rails db:migrate
$ rails server

Navigation with Turbo Drive

Let's take a look at Turbo Drive intercepting link clicks and form submissions so that only changes to the page are rendered, avoiding reload every resource the page uses:

  1. Open your Web Browser
  2. Open the Inspector and switch to its Network tab
  3. Navigate to http://localhost:3000/albums
    • Notice how there are requests to the main html document, CSS files, JS files and other resources
  4. Clear the Network History
  5. Click New Album
    • Notice how there is only a fetch request to /albums/new and no requests for the CSS files, JS files and other resources
  6. Enter a Title and Artist, then click Create Album
    • Notice how there is just a single fetch request (that happens to get redirect)
  7. Click Back to Albums
    • Notice how there is just a single fetch request

Navigation without Turbo Drive

Now we'll disable Turbo Drive and see how a Rails app previously worked without it:

  1. Open app/javascript/application.js and disable Turbo Drive by adding the following line to the bottom of the file:
  • Turbo.session.drive = false
  1. In your browser reload http://localhost:3000/albums
    • Notice how there are requests to the main html document, css files, js files and other resources
  2. Clear the Network History
  3. Click New Album
    • Notice how there are requests to the main html document, css files, js files and other resources
  4. Click Back to Albums
    • Notice how there are requests to the main html document, css files, js files and other resources

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment