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

Created on 24 January 2024, 5 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.

No activities found.

Production build 0.69.0 2024