When browsing an Ajax view, many Drupal.Ajax are created for the refresh feature. Those objects are not cleaned up.

Need to either add some code to clean them up or prevent several objects from being created.

Comments

nod_ created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

christianadamski’s picture

Hey, let's see if I'm having the same issue.

Steps to reproduce:
- create a page view of nodes
- use fields and just the title
- expose a filter, for me it's the node type (article and page)
- enable ajax
- create an attachment view - it can display the same things again
- let the attachment inherit the exposed filter
- now trigger the exposed form a couple times

I notice two things:
- Drupal.ajax() is triggered 2 times more with every button trigger, it starts with 2 calls and then each time goes up by 2
- for every AJAX refresh, a new similar entry in drupalSettings.views.ajaxViews and Drupal.views.instances is created. Each of these is only for the attachment view and gets a new DOM ID. So after 10 triggers, you will have one entry for page_1 and 10 with varying dom_ids for attachment_1

christianadamski’s picture

I'm still debugging through this. My current find is that the expired instances are not removed, because after the Ajax load they have no "element" defined, which in turn is ignored by Drupal.ajax.expired()

No idea is this is relevant though.

christianadamski’s picture

I can confirm that Drupal.views.ajaxView() simply assumes that whatever setting is handed to it also corresponds to an element and just uses the jQuery(selector) result as a given, with actually checking, if that result actually contains anything.

When adding a bailing out function for element not present at the beginning of that function, everything still works fine for me.

christianadamski’s picture

Status: Active » Needs review
StatusFileSize
new521 bytes

Let's see if that change breaks any tests.

christianadamski’s picture

This patch additionally makes sure the RefreshView is only attached once per view. This is already happening before for the exposed form handling and the pager links, but not the manual refresh.

The patch is based on the linked issue #2415027

slasher13’s picture

Status: Needs review » Reviewed & tested by the community

works for me

nod_’s picture

Status: Reviewed & tested by the community » Needs work

Why do you have this condition?

    if (this.$view.length === 0) {
      return;
    }

ajaxView is a constructor, returning early makes a useless object, and seems out of scope for this issue.

Also when changning pages, I would expect the refresh object to be updated to use the new current page the view is at. Looks like we'd refresh the first page only, not the current one.

christianadamski’s picture

@_nod, I agree that this is out of scope for this issue. It's still a problem though. Entries are created for views that are simply not there.

If you confirm that, I would create a new issue.

The issue with the obsolete instances seems to come from the way the SettingsCommand works. It will simply extend the existing settings list of instances. And I do not see any ability to replace instead of extend.

ivavictoria’s picture

@nod_ @ChristianAdamski, for what it's worth, that condition resolves an error I'm having when using an exposed filter: #2797587-4: Uncaught TypeError: Cannot read property 'top' of undefined in ajax_views.js line no 200

If I understand correctly, this condition is simply masking a deeper problem. But, I know next to nothing about this code and can't tell how to fix it.

vagelis-prokopiou’s picture

Patch #7 works for me.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joshtaylor’s picture

I'm also seeing this happen on an exposed filter in a view, and patch #7 fixes it.

caldenjacobs’s picture

#7 works for me as well

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

landsman’s picture

I have this problem on 8.3.6, with Thunder profile.
I added this path:

        "patches": {
            "drupal/core": {
                "Ajax views leave obsolete Drupal.Ajax instances": "https://www.drupal.org/files/issues/2771361-7-do-not-attach-ajax-to-non-present-views.patch"
            }
        },

but it still have problem with this error.

raven.js:367 Uncaught TypeError: Cannot read property 'top' of undefined
    at adjustIfNestedAndOverlapping (contextual.js?v=8.3.6:116)
    at initContextual (contextual.js?v=8.3.6:92)
    at contextual.js?v=8.3.6:205
    at Function.m.each.m.forEach (underscore.js:158)
    at Object.success (contextual.js?v=8.3.6:189)
    at i (jquery.min.js?v=2.2.4:2)
    at Object.fireWith [as resolveWith] (jquery.min.js?v=2.2.4:2)
    at z (jquery.min.js?v=2.2.4:4)
    at XMLHttpRequest.<anonymous> (jquery.min.js?v=2.2.4:4)
    at XMLHttpRequest.d (raven.js:363)

(Raven.js is error handler: https://www.drupal.org/project/raven)

drewcking’s picture

#7 works for me as well, ran into this bug after upgrading from 8.3.6 to 8.3.7

hamrant’s picture

Patch #7 works for me - Drupal 8.3.7.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ayalon’s picture

Here is a reroll for Drupal 8.5

isaacrc’s picture

Patch #7 works for me as well - Drupal 8.4.5 Thanks!

martin107’s picture

As this is a 8.6.x patch, the javascript should not be modified directly

for ajax_view.js there is an equivalent ajax_view.es6.js file.

There is a change record which gives instructions on how to setup a es6 build environment.

martin107’s picture

Here is the change record.

https://www.drupal.org/node/2815083

finnsky’s picture

StatusFileSize
new1.61 KB

Rerolled according ES6

andypost’s picture

Status: Needs work » Needs review

martin107’s picture

StatusFileSize
new1.66 KB
new1.23 KB

the smallest of changes

the AirBnB coding standard which we have adopted replaces single line functions of the form

function() {
  foo('hello');
}

with equivalent code of this form

() =>  foo('hello')

It would be too big an issue to replace all examples in code globally.... but for new code I want to follow the coding standard.

lendude’s picture

Issue tags: +Needs tests

Would be nice to have an automated test for this.

hi_ten_ja’s picture

StatusFileSize
new2.4 KB
new2.4 KB
martin107’s picture

Status: Needs review » Needs work

@hiten2112 Hi,

Could you explain the interdiff please?

It appears only ajax_view.js has been modified, which is not a file which should be modified directly.

ajax_view.es.js is a file that should be changed and then the automated build process reflects the changes to ajax_view.js

In short interdiffs should see matching pairs of changes, one to ajax_view.es.js and then ajax_view.js

This is a dangerous change, because it is not permanent, the next time the build process runs as part of future issue
then changes to only ajax_view_js will be overwritten.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

handkerchief’s picture

Any news about this?

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sandstrom’s picture

Issue tags: -JavaScript +JavaScript

I've updated drupal core to 8.8.0, created a simple view with one exposed filter and enabled Ajax from views configuration. I am getting the same error mentioned below in the browser console when ajax is triggering after hitting the submit button.

Uncaught TypeError: Cannot read property 'top' of undefined at Drupal.AjaxCommands.viewsScrollTop

Tried applying the above patches but none of these worked for me. Composer throwing error "Could not apply patch! Skipping.".

sandstrom’s picture

I added the following codes in my custom views-view.html.twig file, after that views Ajax started working. Somehow attributes were missing from the custom views-view.html.twig file. The view selector was referring to old dom id instead of the new one generated after page refresh.

{%
  set classes = [
    dom_id ? 'js-view-dom-id-' ~ dom_id,
  ]
%}
<div{{ attributes.addClass(classes) }}>
  {# printed all required variables here #}
</div>

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

bbu23’s picture

Patch #28 worked for me.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Version: 9.3.x-dev » 9.1.x-dev
Issue tags: +Bug Smash Initiative
StatusFileSize
new1.74 KB

Re-roll for 9.1.x

rcodina’s picture

Patch on #42 fixes the bug on Drupal 9.2.4. Thanks!

Version: 9.1.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ac’s picture

#42 works on 9.2.9. Also solves #2986041: Triggering RefreshView on a view is making multiple ajax calls which implements an essentially identical fix.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joum’s picture

Status: Needs work » Reviewed & tested by the community

Can confirm #42 also works on core 9.5.11. I propose this is moved to RTBC.

nod_’s picture

Version: 9.5.x-dev » 11.x-dev
Status: Reviewed & tested by the community » Needs work

sorry to do that but we first need a 11.x merge request before committing to other versions of Drupal

_utsavsharma’s picture

StatusFileSize
new882 bytes

Patch for 11.x.

sabrina.liman’s picture

StatusFileSize
new809 bytes

Re-rolling patch #42 for D10.

sabrina.liman’s picture

StatusFileSize
new874 bytes

Re-rolling patch #42 for D10. (With missing var _this) added.

pablo_pukha’s picture

StatusFileSize
new880 bytes

Patch for 11.x

pablo_pukha’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

As mentioned will need a 11.x MR.

Issue summary should be updated with standard issue template as well.

Previously tagged for tests also.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.