why is it still loading

Unloading jQuery in Drupal 8

In the process of redesigning our site, we decided to axe jQuery early on. Analytics informed us that cross-browser compatibility wouldn’t be an issue, so we took the easy performance gain and stuck it out with vanilla JavaScript.

Beginning in version 8, Drupal does not load jQuery by default. Great - no issues for us! However, the long, shadowy spectre of jQuery haunted development.

Even if you’re not using jQuery, it can still load for anonymous users.

Contrib jQuery dependency

You have your libraries.yml file free of any dependencies, and you’re deep into development when you notice jQuery is still loading. Why? Contrib modules can still require jQuery.

For us, the culprit was the Google Analytics module. It’s a great contributed module, but because of the overhead it brought, we decided to create a custom module and attach our tracking code separately.

More to come.

I’m sure there will be other gotchas as D8 proliferates in the wild, but hopefully these two will save you some time in case you find yourself wondering, much like I was, why jQuery still cast its shadow. Now get back to it, and build something awesome.

Correction

Previously, this post stated that Drupal behaviors are dependent on drupal.js, which is in turn dependent on jQuery. This is incorrect and drupal.js in D8 is not dependent on jQuery. After running into some errors, this post on the JavaScript API led me to that conclusion. Apologies and thanks to @nod_ for his tweet and sending me down the right path!

jQuery JavaScript Drupal 8
Ryan Hagerty Headshot

Ryan hails from the Appalachian mountains of West Virginia, and loves creating clean websites made for people to use.