1

In my Drupal 9 site with CKEditor 4 and I have created "media gallery" media type, which displays set of images (using slideshow module). So, when I click "insert media" CKEditor button for tool bar I have media selector popup, I can select and insert media gallery and that works well.

Only problem is that when media is inserted, that AJAX call is cached. So if I for example edit gallery, add another image or remove one and I go to insert that gallery in CKEditor I get old gallery, in state before my recent editing.

When I look at entity media it self it gets updated immediately, but AJAX call CKEditor makes is cached.

How can I disable that caching? Or force cache rebuild when gallery is saved?

Update:

I figured out that at: web/core/modules/media/src/Controller/MediaFilterController.php preview method is setting MaxAge value to 300 (5 minutes).

    // Note that we intentionally do not use:
// - \Drupal\Core\Cache\CacheableResponse because caching it on the server
//   side is wasteful, hence there is no need for cacheability metadata.
// - \Drupal\Core\Render\HtmlResponse because there is no need for
//   attachments nor cacheability metadata.
return (new Response($html, 200, $headers))
  // Do not allow any intermediary to cache the response, only the end user.
  ->setPrivate()
  // Allow the end user to cache it for up to 5 minutes.
  ->setMaxAge(300);

If I change parameter to 0 it starts working well. Now, is there a reason why this caching is needed and what would be the optimal way to solve this issue? I saw that this code has long history, yet since it was a separate module:

https://www.drupal.org/project/entity_embed/issues/3182698

1 Answer 1

1

Solved this by overriding the class and route that was using original's class method. So, I have route subscriber defined like:

services:
  my_module.route_subscriber:
    class: Drupal\my_module\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

Then I have route subscriber it self like:

<?php

namespace Drupal\my_module\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    // Overriding default MaxEge (5 minutes) value of API response.
    if ($route = $collection->get('media.filter.preview')) {
      $route->setDefault('_controller', '\Drupal\my_module\Controller\MyModuleMediaFilterController::preview');
    }
  }

}

And finally I have class override:

<?php

namespace Drupal\my_module\Controller;

use Drupal\filter\FilterFormatInterface;
use Drupal\media\Controller\MediaFilterController;
use Symfony\Component\HttpFoundation\Request;

/**
 * MediaFilterController override.
 */
class MyModuleMediaFilterController extends MediaFilterController {

  /**
   * Returns an HTML response containing a preview of the text after filtering.
   */
  public function preview(Request $request, FilterFormatInterface $filter_format) {
    // Overriding default MaxAge value of 300 (5 minutes).
    return parent::preview($request, $filter_format)->setMaxAge(0);
  }

}

Hopefully this will help someone and save some time. Maybe some condition can be added to preview override, like remove caching only for back-end or similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.