Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDST-728: Add in no-cache cache control directive. #13

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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