- š§šŖBelgium wim leers Ghent š§šŖšŖšŗ
I'm 99% certain that this is another occurrence of š Big Pipe calls attachBehaviors twice Closed: works as designed ā i.e. a bug in Gin's Drupal behaviors not using
once()
correctly. - Status changed to Postponed: needs info
almost 2 years ago 10:12am 15 February 2023 - šØšSwitzerland saschaeggi Zurich
Is it? @Joao Sausen & oliverh65 is this happening with Gin or another theme?
If so we can close it as a duplicate in favor of š Double rendering select all checkboxes Closed: cannot reproduce
- š«š·France olivierh65
This behavior is not Gin specific.
I think it's a big pipe problem, as I've had this problem in leaflet module. - šØšSwitzerland saschaeggi Zurich
Then let's move this back to core š
- Status changed to Active
over 1 year ago 3:16pm 15 February 2023 - š§šŖBelgium wim leers Ghent š§šŖšŖšŗ
It's starting to sound like a bug in the Leaflet module then actually š Can you provide concrete steps to reproduce?
It's normal that
attachBehaviors
is called multiple times. Also see the discussion in š Big Pipe calls attachBehaviors twice Closed: works as designed .Just checked https://git.drupalcode.org/project/leaflet/-/blob/10.0.x/js/leaflet.drup... and I see:
Drupal.behaviors.leaflet = { attach: function(context, settings) { // For each Leaflet Map/id defined process with Leaflet Map and Features // generation. $.each(settings.leaflet, function(m, data) { $('#' + data['mapid'], context).each(function () {
ā¦ this is definitely incorrect. It should be using
once()
. It doesn't use that anywhere in the JS file.It was correctly updated in https://git.drupalcode.org/project/leaflet/-/commit/73757ffd5e5565a606f6.... Looks like there's a history of breaking this: https://www.drupal.org/project/issues/leaflet?text=attachbehaviors&statu... ā
- Status changed to Postponed: needs info
over 1 year ago 10:10pm 9 May 2023 - š®š¹Italy itamair
Thank @Wim Leers for pointing that out,
but I believe that code in the Leaflet module is correct (and at least is not wrong).
The Leaflet module js is handling all the each defined Leaflet map for the web page, and processing those settings (and related Leaflet maps) accordingly. We deliberately didn't want/need to use the once() pattern there ... and the Leaflet module already faced an issue related to Big Pipe causing double attachments, here: https://www.drupal.org/project/leaflet/issues/3337537 š Big Pipe changes causes multiple map attachments Fixed
and this following comment explained how in more details: https://www.drupal.org/project/leaflet/issues/3337537#comment-14898998 š Big Pipe changes causes multiple map attachments FixedIn the end, if the Big Pipe triggers twice the Drupal.behaviour ... well it could be right not to stop its second call.
What we need to stop is the re-initialisation of a Leaflet Map inside a container that already have an initialised one.I believe that is not the Leaflet module triggering double Drupal.attachBehaviours() execution ... but it is just facing it.
Please send me here better evidence that all this is caused by the Leaflet module indeed (that I couldn't see yet with your comment),
otherwise I will close this as "works as designed" soon ... - Status changed to Needs work
over 1 year ago 11:10am 10 May 2023 - š§šŖBelgium wim leers Ghent š§šŖšŖšŗ
Note that none of this is BigPipe-specific. It's just the Drupal AJAX system.
believe that is not the Leaflet module triggering double Drupal.attachBehaviours() execution
Correct, it's the AJAX system, which BigPipe uses.
... but it is just facing, and properly reacting to it.
Leaflet is indeed invoked to attach behaviors. But it's not reacting to this properly.
Quoting
Drupal.attachBehaviors
fromcore/misc/drupal.js
:* Behaviors should use `var elements = * once('behavior-name', selector, context);` to ensure the behavior is * attached only once to a given element. (Doing so enables the reprocessing * of given elements, which may be needed on occasion despite the ability to * limit behavior attachment to a particular element.)
š It's that reprocessing that Leaflet does not handle correctly.
We deliberately didn't want/need to use the once() pattern there ...
Interesting ā¦ š¤
#3337537-4: Big Pipe changes causes multiple map attachments ā says:
With the above solution the once() condition will be triggered only for the all document load ("html"), and that is not good.
Well, that's true, one should never use
once()
on the entire document. Then this is indeed a logical consequence:It won't trigger the Leaflet Map initialisation in case of Leaflet Map inside Ajax requests and contexts
You need to use the proper selector ā see https://www.npmjs.com/package/@drupal/once, which says:
once(id, selector, [context]) ā Array.<Element> Ensures a JavaScript callback is only executed once on a set of elements.
So for example
Drupal.behaviors.contextual
does this:Drupal.behaviors.contextual = { attach(context) { const $context = $(context); // Find all contextual links placeholders, if any. let $placeholders = $( once('contextual-render', '[data-contextual-id]', context), );
That means
'contextual-render'
is the identifier of the behavior/processing you want to do only once,'[data-contextual-id]'
is the selector to find the relevant elements andcontext
is the root of the tree where you want to find elements matching that selector.The patch at #3337537-2: Big Pipe changes causes multiple map attachments ā did this though:
once('myLeafeltBehavior', 'html').forEach(function (element) {
ā¦ which means it was selecting the
html
element ā¦ which selects the entire HTML document, so it's clearly not a selector matching every individual leaflet map!Hope it makes sense now š
- Status changed to Needs review
over 1 year ago 3:23pm 10 May 2023 - š®š¹Italy itamair
cool! ... and super thanks for your great mentoring @Wim š
ket me test the attached patch, on some advance Leaflet use case (multiple leaflet maps in the same page, and added through IEF) and I will commit into dev and deploy a new release with this ... -
itamair ā
committed 1a12d250 on 10.0.x
Issue #3314762 by itamair, Christopher Riley, Wim Leers: Leaflet not...
-
itamair ā
committed 1a12d250 on 10.0.x
- Status changed to Fixed
over 1 year ago 4:17pm 10 May 2023 - š®š¹Italy itamair
Ok. I successfully tested and QAed what has been committed into dev. I am going to deploy a new Leaflet 10.0.12 release with this.
Thanks again @Wim Leers for guiding me to this, that I am confident is a proper fix for this. Automatically closed - issue fixed for 2 weeks with no activity.