Make ViewAjaxResponse cacheable

Created on 26 February 2025, 10 days ago

Problem/Motivation

AJAX responses from views are never cached by reverse-proxies like Varnish. That's because ViewAjaxResponse does not extend CacheableAjaxResponse so it has no cacheability metadata.

Now that AJAX uses GET requests ( 📌 Allow AJAX to use GET requests Fixed ) it should be possible to make ViewAjaxController return a cacheable response.

Steps to reproduce

  1. Create an AJAX view with multiple pages.
  2. Enable http.response.debug_cacheability_headers.
  3. Trigger an AJAX request by using the pager.
  4. Inspect the AJAX response headers.
    It has these headers:
    x-drupal-cache: UNCACHEABLE (no cacheability)
    x-drupal-dynamic-cache: UNCACHEABLE (no cacheability)
    

Proposed resolution

ViewAjaxResponse should extend CacheableAjaxResponse.
And ViewAjaxController should apply the cache metadata from the view to the response.

The views_ajax_get module does this and could be used as inspiration: https://git.drupalcode.org/project/views_ajax_get/-/blob/f54e8f83db62dda...

I also see some work was started here: https://www.drupal.org/project/drupal/issues/2500313#comment-12294510 📌 Add views render caching on views ajax requests Closed: outdated

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Feature request
Status

Active

Version

11.1 🔥

Component

views.module

Created by

🇫🇷France prudloff Lille

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