Introduction to Turbo: Build dynamic web applications without writing JavaScript
Ever heard of programming with Turbo? You can do it all without writing JavaScript. See how it works and what the benefits are.

Editorial translation by Impulso.
Single-page application (SPA) architecture dominated the past decade, driven by libraries like React. Now there’s an alternative: Turbo. It gives you the responsiveness and smooth experience of an SPA without having to write JavaScript. Never heard of it? Let’s take a look at how it works.
What is Turbo?
Released in September 2021 by the creators of Ruby on Rails, Turbo is a lightweight JavaScript library under 400kb. It’s also easy to use, with a gentle learning curve. And yet it’s very powerful. With it, you can build dynamic web pages that behave like SPAs without using JavaScript. That makes development more productive and less complex.
Part of the simplicity comes from the fact that with Turbo, you don’t have to manage state, authentication and authorization, routing, and so on. It receives HTML that the server has already rendered and simply inserts it into the page.
But it does this in a very clever way. With Turbo Drive, navigation is smooth and pages don’t reload. Instead, parts of the page can be updated independently with Turbo Frame, and dynamic updates can be made using Turbo Stream.
To get a better sense of how it all works, let’s take a closer look at each of these three components.
Turbo Drive: Smooth navigation
To load pages smoothly, Turbo Drive monitors clicks on and intercepts form submissions by preventing the browser’s default behavior. Then it updates the URL using the History API and requests the new page using the fetch function. Finally, it receives the HTML and inserts it into the page.
All of this is done efficiently. The contents of the are replaced, while the is merged, preserving the application’s state. The experience is as smooth for the user as it would be in an SPA.
Often, though, a user action changes only a small part of the page. Say we have a to-do list app. When we mark an item as done, only that item’s state changes. So there’s no need to change the entire .
To avoid doing that, let’s move on to the second component.
Turbo Frame: Updating parts of a page
Turbo Frame lets you update individual parts of the page. To do this, we wrap the element in a frame. Say the example below is our to-do item:

When the user clicks Done, we want only that item to update, changing the link to Undone. To make that happen, the server should respond with a, as shown below:

That way, Turbo Drive replaces only the. The request is faster because there’s much less content to load than with a full page.
If the list is very long, you can defer loading items that aren’t visible to the user by adding the src attribute to the:

For our last example, imagine we have a list with 2 items and want to delete one of them. When we click Delete, we need to remove the element from the page:

This is where Turbo Stream comes in handy.
Turbo Stream: Real-time updates
Turbo Stream lets you add, remove, and modify elements anywhere on the page using server responses, WebSocket messages, or any other transport.
In the previous example, when we click Delete for the “Aprender Rails” item, the server sends the following response containing the:

If we also want the item to disappear in real time for everyone else viewing the list, we can send the action over WebSocket. In Ruby on Rails, we first create the WebSocket channel that will receive updates about the items:

Then we update our model so that whenever an item is removed, it broadcasts the removal to everyone connected to the items channel:

What does this look like in practice?
With these changes, the removal is broadcast in real time to everyone connected to the page that lists the items. The result will look something like this: 
Besides removing elements, you can perform the following actions:
- - prepend: Inserts content at the beginning of the target element.
- - append: Adds content at the end of the target element.
- - replace: Replaces the target element with the supplied content.
- - update: Updates the content inside the target element.
- - before: Inserts content immediately before the target element.
- - after: Adds content immediately after the target element.
The future of Turbo
So it’s clear that Turbo offers a simpler, less complex approach than traditional SPAs. After all, it keeps all the logic on the server. That does away with the need for JSON, state management, build tools like Webpack, and other things that complicate front-end development today.
Now that you know how it all works, it’s time to build your skills. I recommend getting involved in a community where you can ask questions and share experiences with other professionals. Want a recommendation? Check out Impulso’s Discord server. It’s well worth it.
All in all, the future of web development may be less complicated than we think. That’s why Turbo is worth a try. I guarantee you’ll be surprised by how easy it is and how productive you can be when building dynamic web applications without writing a single line of JavaScript.
Written by an Impulser Professional. If you’d like to contribute an article, too, send an email to blog@impulso.team


