- π³π±Netherlands kingdutch
Hopefully π± [Meta] Allow StreamWrapper's to provide cacheability metadata Active can fix this.
- πΊπΈUnited States grasmash
can you leverage this? https://www.drupal.org/project/cache_control_override β
- πΊπΈUnited States cmlara
RE:CCO
I do not believe so.
Firstly because there is currently no ability to even provide metadata for caching from a StreamWrapper, meaning there is nothing to bubble up to CCO.
Secondly:
IIRC CCO has known problems that can only be solved by Core fixing its metadata handling problems - πΊπΈUnited States grasmash
I found a workaround for this that is good enough for my own needs. Basically, you need to user alter hooks to add cachebility metadata at the entity level. In my case, I wanted to add a max-age to a node when it had a file field with a presigned url that was set to expire.
This could be simplified. I had two different fields I had to take into account. Posting for posterity:
/** * Implements hook_node_load(). */ function mymodule_node_load($entities) { $map = [ // node-type => s3 key. 'song' => [ 's3_key' => 'songs', // We only override the max-age if one of the file fields in actually populated. 'required_fields' => ['field_song_audio', 'field_song_archive'], ], 'firmware_release' => [ 's3_key' => 'firmware-releases', 'required_fields' => ['field_firmware_archive'], ] ]; foreach ($entities as $entity) { if ($entity->getEntityTypeId() == 'node' && in_array($entity->getType(), array_keys($map))) { if (!shouldOverrideCacheHeaders($map, $entity)) { continue; } $config = \Drupal::config('s3fs.settings'); if ($config->get('presigned_urls')) { $presigned_urls = getPresignedUrls($config->get('presigned_urls')); $s3_key = $map[$entity->getType()]['s3_key']; foreach ($presigned_urls as $blob => $timeout) { if (preg_match("^$blob^", $s3_key)) { $cachebility_metadata = new CacheableMetadata(); $cachebility_metadata->setCacheMaxAge((int) $timeout); $entity->addCacheableDependency($cachebility_metadata); break; } } } } } } /** * @param $map * @param $entity * * @return bool */ function shouldOverrideCacheHeaders($map, $entity): bool { foreach ($map[$entity->getType()]['required_fields'] as $field_name) { if (!$entity->{$field_name}->isEmpty()) { return TRUE; } } return FALSE; } /** * @param string $presigned_urls_config * * @return array * @see \Drupal\s3fs\StreamWrapper\S3fsStream->__construct()) */ function getPresignedUrls(string $presigned_urls_config): array { $presigned_urls = []; foreach (explode(PHP_EOL, $presigned_urls_config) as $line) { $blob = trim($line); if ($blob) { if (preg_match('/(.*)\|(.*)/', $blob, $matches)) { $blob = $matches[2]; $timeout = $matches[1]; $presigned_urls[$blob] = $timeout; } else { $presigned_urls[$blob] = 60; } } } return $presigned_urls; }