Skip to content

Commit

Permalink
Merge pull request #13 from discoverygarden/feature/cache-revalidation
Browse files Browse the repository at this point in the history
DDST-728: Add in `no-cache` cache control directive.
  • Loading branch information
nchiasson-dgi authored Nov 26, 2024
2 parents a089fce + 2288350 commit 8c2ca57
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions iiif_presentation_api.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ services:
class: Drupal\iiif_presentation_api\FieldMapper
tags:
- { name: iiif_presentation_api_mapper, base: iiif_presentation_api_map, version: v3 }

iiif_presentation_api.revalidation_directive_subscriber:
class: Drupal\iiif_presentation_api\EventSubscriber\RevalidationDirective
tags:
- { name: event_subscriber }
7 changes: 6 additions & 1 deletion src/Controller/V3/ManifestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\iiif_presentation_api\EventSubscriber\RevalidationDirective;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function build(string $parameter_name, RouteMatchInterface $route_match)
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => $cache_meta,
];
$serialized = $this->serializer->serialize($_entity, 'iiif-p-v3', $context);
return (new CacheableJsonResponse(
$response = (new CacheableJsonResponse(
$serialized,
200,
[
Expand All @@ -73,6 +74,10 @@ public function build(string $parameter_name, RouteMatchInterface $route_match)
],
TRUE
))->addCacheableDependency($cache_meta);

$response->headers->set(RevalidationDirective::HEADER, TRUE);

return $response;
});

if (!$context->isEmpty()) {
Expand Down
53 changes: 53 additions & 0 deletions src/EventSubscriber/RevalidationDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Drupal\iiif_presentation_api\EventSubscriber;

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

/**
* Add 'Cache-Control: no-cache' to IIIF-P manifests.
*
* This is done such that access control actions should take effect sooner, at
* the expense of more load on the server during general use.
*
* Note: This is explicitly weighted/prioritized such that it should run _after_
* Drupal's `FinishResponseSubscriber` runs to add its base cache headers.
*
* @see \Drupal\Core\EventSubscriber\FinishResponseSubscriber::getSubscribedEvents()
*/
class RevalidationDirective implements EventSubscriberInterface {

const HEADER = 'X-iiif_presentation_api_revalidate';

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() : array {
return [
KernelEvents::RESPONSE => [['doRevalidation', -10]],
];
}

/**
* Event callback; add in 'must_revalidate' for our controller response.
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The response on which to act.
*/
public function doRevalidation(ResponseEvent $event) : void {
$response = $event->getResponse();

if (!$response->headers->has(static::HEADER)) {
return;
}

$response->headers->remove(static::HEADER);

if ($response->isCacheable()) {
$response->setCache(['no_cache' => TRUE]);
}
}

}
2 changes: 1 addition & 1 deletion src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BaseImageBodyEventSubscriber implements EventSubscriberInterface {
* Constructor.
*/
public function __construct(
protected PluginManagerInterface $idPluginManager
protected PluginManagerInterface $idPluginManager,
) {
}

Expand Down

0 comments on commit 8c2ca57

Please sign in to comment.