Kalan's Blog

Software Engineer / Taiwanese / Life in Fukuoka

Current Theme light

Introduction

DHH (the creator of Ruby on Rails) tweeted about his new creation, Hotwire. DHH is a fan of Single-Page Applications (SPAs) (as can be inferred from the tweet), so he tries to minimize the use of JavaScript in development. This tweet sparked a lively discussion on Twitter, and here is a brief summary.

Here is a direct quote from the official introduction to Hotwire:

Hotwire is an alternative approach to building modern web applications without using much JavaScript by sending HTML instead of JSON over the wire.

This description has two key points:

  • No need to use JavaScript extensively.
  • Sending HTML instead of JSON directly.

This concept is not new; Ruby on Rails has been using a similar approach called Turbolinks for several years.

Turbolinks is a JavaScript library that is commonly used with Ruby on Rails (but can also be used as a standalone library). Its main purpose is to avoid the need for full page reloads and re-requesting CSS by fetching and swapping HTML directly. It is not entirely accurate to say "no need to use JavaScript" because JavaScript is still there, but the library takes care of it for you, allowing you to develop without writing JavaScript code.

For example, if there is a tag like this on a page:

<a href="/articles/1" data-remote="true">link</a>

If Turbolinks is enabled in Ruby on Rails, when a user clicks the link, it won't actually send a new request. Turbolinks will do something like this:

fetch('/index.html').then(res => res.html())
	.then((html) => $page.html(html))

So, when a user clicks the link, instead of reloading the HTML, Turbolinks fetches the HTML file (via AJAX) and renders it directly using JavaScript. Ruby on Rails integrates tightly with Turbolinks, so sometimes you hardly notice the presence of Turbolinks; you just feel like "Wow, the page transition seems faster."

The effect may become less noticeable as the HTML size increases, but if the HTML is not too large, it can provide a better user experience.

Why is this approach useful?

  • No need to reload CSS and JavaScript when transitioning between pages, resulting in a better user experience.
  • Turbolinks handles the head section for you.
  • Minimal additional JavaScript code is required.
  • Backend engineers can achieve a better experience with minimal effort.

Things to be aware of

  • Events like load may only trigger on the initial page load because Turbolinks doesn't reload the entire page. Remember to switch to using turbolinks:load or similar event listeners.
  • As JavaScript interactions increase, there may be conflicts between event bindings.
  • State is not cleared when transitioning between pages, so poorly written JavaScript can lead to memory leaks.

When JavaScript interactions become more complex, using Turbolinks may sometimes result in unexpected behavior, such as errors caused by repeated execution.

Some thoughts

As developers, many of us have experience with building SPAs, and we know that creating a functional and user-friendly SPA is not easy. Issues like inefficient memory usage, out-of-sync state, poor error handling, and loading large JavaScript bundles can arise. In contrast, using server-side rendering (SSR) with Turbolinks to enhance the user experience seems like a better approach.

Afterword

Some people believe that JSON is more efficient than HTML for transmission, but once gzipped, they are often similar in size, and sometimes HTML is even smaller. So, rendering HTML directly doesn't seem like such a terrible thing.

However, currently, Turbolinks is most seamlessly integrated with Ruby on Rails, and Hotwire is relatively new, so some developers may still be observing its development.

Prev

End of 2020, to talk about what a computer is

Next

2020 Review/2020 Year in Review — Technology

If you found this article helpful, please consider buy me a drink ☕️ It'll make my ordinary day shine✨

Buy me a coffee

作者

Kalan 頭像照片,在淡水拍攝,淺藍背景

愷開 | Kalan

Hi, I'm Kai. I'm Taiwanese and moved to Japan in 2019 for work. Currently settled in Fukuoka. In addition to being familiar with frontend development, I also have experience in IoT, app development, backend, and electronics. Recently, I started playing electric guitar! Feel free to contact me via email for consultations or collaborations or music! I hope to connect with more people through this blog.