Bug with ajax and caching

Created on 31 March 2025, about 1 month ago

Problem/Motivation

Hi!
I've noticed bug when views page uses selective exposed filter and enabled ajax.
After clearing cache and refreshing page 3 times there are two sets of data for the same view in the ['drupalSettings']['views']['ajaxViews'] (one is cached and another is newly generated) and it then breaks ajax refreshing of the results - the scrollTop js command receives wrong view_dom_id and errors on empty element.
After removing the "selective" filter and clearing cache the bug doesn't appear again until re-adding the filter to the view.

Example of the 'views' drupalSettings:

 "views": {
    "ajax_path": "\/views\/ajax",
    "ajaxViews": {
      "views_dom_id:fb25aa069521327027be9031f3993ccbe6db057f83ffad6b8d7761d84a9797f5": {
        "view_name": "user_bids",
        "view_display_id": "open_all_bids",
        "view_args": "7",
        "view_path": "\/user\/7\/buying",
        "view_base_path": "user\/%user\/buying",
        "view_dom_id": "fb25aa069521327027be9031f3993ccbe6db057f83ffad6b8d7761d84a9797f5",
        "pager_element": 0
      },
      "views_dom_id:20a922655c8662f7e8c74e8365218a2114b88433ff4956a9abd40301e0fc13b7": {
        "view_name": "user_bids",
        "view_display_id": "open_all_bids",
        "view_args": "7",
        "view_path": "\/user\/7\/buying",
        "view_base_path": "user\/%user\/buying",
        "view_dom_id": "20a922655c8662f7e8c74e8365218a2114b88433ff4956a9abd40301e0fc13b7",
        "pager_element": 0
      }
    }
  },

Any ideas how to fix this bug? Maybe we need to add/change something in Selective::getViewCopy()?

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine khiminrm

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

Comments & Activities

  • Issue created by @khiminrm
  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    Can't find what causes the bug. But noticed that it not happens if I return empty results just before $value = $style->getField($row->index, $field_id); in Selective::getOids() like this:

              return [];
              $value = $style->getField($row->index, $field_id);
         

    If I add return []; after $value = $style->getField($row->index, $field_id);, the error appears again. So during rendering somehow the drupalSettings receives newly generated view_dom_id and cached.

    I've just used custom fix to remove doubled view_dom_id:

    function my_module_js_settings_alter(array &$settings, AttachedAssetsInterface $assets) {
      if (empty($settings['views']['ajaxViews']) || count($settings['views']['ajaxViews']) == 1) {
        return;
      }
      // Fix bug with doubled view_dom_id for views with selective filters.
      $view_name_display_dom_ids = [];
      foreach ($settings['views']['ajaxViews'] as $dom_id => $ajax_view) {
        $view_name_display_dom_ids[$ajax_view['view_name']][$ajax_view['view_display_id']][] = $dom_id;
      }
      foreach ($view_name_display_dom_ids as $displays) {
        foreach ($displays as $dom_ids) {
          if (count($dom_ids) > 1) {
            unset($settings['views']['ajaxViews'][$dom_ids[0]]);
          }
        }
      }
    }

    But would be nice to fix source of the issue.

Production build 0.71.5 2024