0

I'd like my JSON:API responses to return the e-tag header so that consuming remote sites can determine (e.g. with a HEAD request) if they need to ditch their cached responses and re-fetch. There is no last-modified or e-tag headers being sent at all - for any Drupal page for that matter. How can I enable this? Thanks!

I do see X-Drupal-Dynamic-Cache and X-Drupal-Cache headers, but this isn't enough to go on.

Running Drupal 8.6.10 and JSON:API 8.x-2.4.

2 Answers 2

1

I changed the "Browser and proxy cache maximum age" setting in Configuration > Development > Performance and it appeared to do the trick. It was previously set to "no caching".

0
  1. Create a new module (etag)
  2. Add etag.services.yml
   services:
      etag.response_subscriber:
        class: Drupal\etag\EventSubscriber\ResponseSubscriber
        tags:
          - { name: event_subscriber }
  1. Create ResponseSubscriber class inside modules/custom/etag/src/EventSubscriber/ResponseSubscriber.php
<?php

namespace Drupal\etag\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

class ResponseSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents(): array {
    return [
      KernelEvents::RESPONSE => 'onResponse',
    ];
  }

  /**
   * This method is called whenever the KernelEvents::RESPONSE event is dispatched.
   *
   * @param ResponseEvent $event
   */
  public function onResponse(ResponseEvent $event): void {
    $response = $event->getResponse();
    $etag_value = '"' . md5($response->getContent()) . '"';

    $response->headers->set('ETag', $etag_value);

  }
}

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.