JavaScript in Rails: From Rails 6 to Rails 7
Henry Nawrocki
Ruby on Rails Tech Lead | Driving Innovation and Business Growth | Full-Stack Developer
When diving into the Rails framework, one can't help but notice the evolution in how it handles JavaScript. Between Rails 6 and Rails 7, we've witnessed a significant paradigm shift. Let’s break down these changes:
1. Rails 6 and Webpacker:
In Rails 6, Webpacker became the default JavaScript compiler, replacing the old asset pipeline for JavaScript. It provided Rails developers with the power of modern JavaScript tools, libraries, and practices. With Webpacker, integrating frameworks like React, Vue, and Angular became more accessible. But while powerful, it also added a layer of complexity for those who just wanted simple JS functionalities.
2. Rails 7, Hotwire, and Import Maps:
With Rails 7, a new approach emerged. DHH, the creator of Rails, introduced Hotwire. The idea is to offer a way to build modern, interactive applications without requiring significant amounts of JavaScript. Instead of sending JSON and rendering it via JavaScript, Hotwire sends HTML updates. This means, for many applications, less reliance on heavy front-end frameworks.
Alongside Hotwire, Rails 7 brought in "import maps." Import maps allow developers to import JavaScript modules using ESM (ECMAScript Modules) without needing a bundler. This is a step back towards simplicity, eliminating the need for Webpacker in many applications.
Example:
Imagine you have a "like" button on a blog post. Traditionally, when you click it, you might use AJAX to send a request, get a JSON response, and then use JavaScript to update the number of likes on the page.
With Hotwire:
The result? The button updates almost instantly, similar to an SPA, but without writing the custom JavaScript logic to handle the JSON and update the DOM. You essentially get the interactivity of an SPA with the simplicity and server-rendered benefits of a traditional Rails app.
In Conclusion:
The journey from Rails 6 to Rails 7 has been one of introspection for the Rails community. It revisits the essence of Rails—convention over configuration—and seeks to minimize unnecessary complexities. While Webpacker opened doors to the vast JavaScript ecosystem, Hotwire and import maps in Rails 7 aim to simplify and, in some cases, reduce the need for JavaScript. This evolution not only impacts Rails developers but also offers insights into the broader trajectory of web development practices.
Which approach resonates more with you? Are you a fan of the rich tooling that Webpacker brought, or are you excited about the simplicity that Hotwire and import maps promise? Whatever the preference, Rails continues to be at the forefront of web development, guiding the conversation on best practices.
S R E | DevOps en FLUZO
10 个月Henry Nawrocki, ?importmaps seems awesome, but I would like to know some way to import a simple javascript file with some simple javascript funtions but still passing by the assets pipeline. I wrote about this https://stackoverflow.com/questions/77763801/rails-7-how-to-add-custom-javascript-functions. Is posible? Thanks.
I help Rails Shops ship public APIs and integrations so they can focus on their fantastic app.
1 年I’m all into rich stuff ?? But this time I’d pick Hotwire if I were to choose.
Full-stack Web Developer | ReactJS, NextJS, NodeJS, Ruby on Rails | Electrical and Computer Enginneer | Delivering Seamless Web Solutions for Global Clients | Passionate Problem Solver
1 年Hotwire