Don't tie jQuery to the Rails

rail road tracks Photo by Kevin Moreira licensed under CC. The photo has been modified.

It’s time for jQuery to go. It’s a great library but it’s not a part of Rails.

I was recently auditing a site for dependencies. Skimming the list of JavaScript libraries I blew right by jQuery. Then a thought popped into my head. The bits of dynamic front-end were built using React. Where was jQuery being used?

jQuery joins Rails

Rails 3.1 was unleashed in 2011. With it came a switch from Prototype to jQuery. At that time everyone was using jQuery. It was the only thing that made JavaScript even slightly palatable.

Rails leveraged it to create unobtrusive JavaScript (UJS). It still powers features like remote forms, the data-confirm attribute, and CSRF protection.

Life was good.

The fall of jQuery

When jQuery arrived it was amazing. It worked across browsers. Ajax was simple. Events were handled sanely. CSS selectors saved us all from the hell of DOM navigation.

Today, many of these features are built into JavaScript. Developers are avoiding manual DOM manipulation in favor of frameworks like Angular, Ember, and React. jQuery has shifted from required to optional.

That optional dependency is 32kB minified and gzipped. The only thing Rails needs it for is 556 lines of UJS. I decided to suggest removing it.

Cutting it loose from the Rails

As it turned out, Benoit Bénézech had beat me to it. He opened an issue on jquery-ujs outlining its removal. Rails projects use Google Groups rather than GitHub issues to discuss features so it was closed. Unable to find a post about it in the Rails core group I opened one.

I received a quick reply. Rails was considering this as a Google Summer of Code project. It turns out having original ideas is tricky.

So, it looks like 2016 might be the year Rails and jQuery go their separate ways. In the mean time you can try out Vanilla UJS. It’ll be smaller for now and with any luck it won’t be need for long.