0

I have a custom module for an online shop. Module's controller gets a product id which is sent by the user through a query string and puts it into the session. That is crucial for the shopping cart functionality.

Things are that, after clearing the cache, everything is ok, but after a few minutes/requests, the requests stop entering the controller. It seems like the response has been cached, even if:

  • I have disabled the caches at admin/config/development/performance,
  • I return '#cache' => ['max-age => 0] within the render array returned by the controller and
  • all responses come with the header "x-drupal-cache-max-age: 0 (Uncacheable)".

I also noticed that these errors usually come with the following apache error:

AH01070: Error parsing script headers
4
  • The error is probably only because you are trying to debug caching by enabling http.response.debug_cacheability_header. Otherwise this might be helpful drupal.stackexchange.com/questions/230169/…
    – 4uk4
    Commented Jul 7, 2022 at 11:31
  • I think max-age => 0 doesn’t bubble properly in all cases without a patch. You can use query strings as cache contexts for better performance.
    – mona lisa
    Commented Jul 7, 2022 at 11:59
  • @cilefen you are right. If you want to disable cache on a controller you need to use this method: public function getCacheMaxAge(): int { return 0; } Commented Jul 9, 2022 at 6:08
  • @4uk4 This was my problem! Thank you very much.
    – fombo
    Commented Jul 9, 2022 at 10:25

1 Answer 1

0

If you want to disable the caching of a controller, use this method in your controller class:

  /**
   * @return int
   */
  public function getCacheMaxAge(): int {
    return 0;
  }
2
  • Good to know, thanks.
    – fombo
    Commented Jul 9, 2022 at 10:27
  • Not necessary when you already return cache max-age in the render array and also it has no effect because a controller doesn't implement this method.
    – 4uk4
    Commented Jul 9, 2022 at 16:21

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.