Multisteps form - Browser back button with Chrome - Ajax out of control

Created on 7 March 2024, 9 months ago
Updated 20 April 2024, 7 months ago

Problem/Motivation

I came across a situation where, the code in the commit is causing another ajax call (which loads content to a block) in the same page of the webform, to fire in every 300 milliseconds in a continuous loop.

Steps to reproduce

  1. Create a webform, with ajax enabled.
  2. In a separate module, create ajax code that will call a path and load the contents to a block, and also calls Drupal.attachBehaviors() to reattach the Drupal behaviors.
  3. In Chrome: Go to the webform submission form and enter some data.
  4. Then navigate away to some other page in website.
  5. Then click the browser backbutton.
  6. Now check the block where ajax code is called, and check the browser developer tools Network tab.
  7. We can notice, repeated ajax calls from our module's block.

Proposed resolution

Not deeply tested, but the below change fix the issue.

diff --git a/js/webform.behaviors.js b/js/webform.behaviors.js
index 39c67ffa8..8ef6b852e 100644
--- a/js/webform.behaviors.js
+++ b/js/webform.behaviors.js
@@ -29,12 +29,9 @@
 
     // If the back button is pressed, delay Drupal's attaching of behaviors.
     if (backButton) {
-      var attachBehaviors = Drupal.attachBehaviors;
-      Drupal.attachBehaviors = function (context, settings) {
-        setTimeout(function (context, settings) {
-          attachBehaviors(context, settings);
-        }, 300);
-      };
+      setTimeout(function () {
+        Drupal.attachBehaviors(document.querySelector('#webform-container'));
+      }, 300);
     }
   }
🐛 Bug report
Status

Fixed

Version

6.2

Component

Code

Created by

🇮🇳India kevinsiji

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024