Turbo: Remove behaviours from Drupal.behaviors when Turbo removes their <script> elements?

Created on 24 January 2024, 10 months ago

Problem/Motivation

The main problem this causes is that behaviours can continue to attach to a page after a Turbo visit, making DOM changes and inserting elements that likely won't do anything. This is exacerbated by Turbo 8.0.0-beta.3 which added a feature to automatically remove unused CSS files when performing a visit to a new page via Turbo; this feature is a good addition, but it means that any elements added to the DOM via one of these lingering behaviours that expects certain styles to be present to hide them until needed will no longer be present, possibly resulting in broken-looking pages.

Steps to reproduce

See above.

Proposed resolution

I'm not really sure. This is a difficult one because we can't predict which JavaScript files add behaviours, and because of the way that behaviours are just added as properties to the Drupal.behaviors global, there's no abstraction layer that we could insert ourselves into to intercept such additions and make note of what gets added and when. This is where something similar to the dependency injection container but in Drupal's JavaScript would be immensely useful.

Remaining tasks

¯\_(ツ)_/¯

Maybe override Drupal.behaviors with our own implementation that notifies us when properties are added and combine that with a Mutation Observer? That's the best I can think of at the moment.

User interface changes

None, other than less broken stuff.

API changes

None?

Data model changes

None, well, probably none.

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇨🇦Canada ambient.impact Toronto

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024