Kalan's Blog

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

四零二曜日電子報上線啦!訂閱訂起來

Software Engineer / Taiwanese / Life in Fukuoka
This blog supports RSS feed (all content), you can click RSS icon or setup through third-party service. If there are special styles such as code syntax in the technical article, it is still recommended to browse to the original website for the best experience.

Current Theme light

我會把一些不成文的筆記或是最近的生活雜感放在短筆記,如果有興趣的話可以來看看唷!

Please notice that currenly most of posts are translated by AI automatically and might contain lots of confusion. I'll gradually translate the post ASAP

Hotwire and Turbolinks

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