Skip to content

Commit

Permalink
BUGS-5816: Resolves issue where changing file content would not serve… (
Browse files Browse the repository at this point in the history
#52)

* BUGS-5816: Resolves issue where changing file content would not serve new files.

Co-authored-by: Ryan Wagner <[email protected]>
Co-authored-by: Nicole Sciacca <https://git.drupalcode.org/nsciacca>
  • Loading branch information
rwagner00 and Ryan Wagner authored Mar 26, 2024
1 parent 96d5b49 commit 9593304
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pantheon_advanced_page_cache.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Drupal\Core\Entity\EntityInterface;
use Drupal\image\Entity\ImageStyle;

/**
* Implements hook_ENTITY_TYPE_update() for file entities.
*
* Clears edge cache by file url. If this is not done, changing file contents
* does not actually change the file that gets served until the cache times out.
*/
function pantheon_advanced_page_cache_file_update(EntityInterface $file) {
// Don't try this if we're not in a Pantheon environment.
if ( ! function_exists('pantheon_clear_edge_paths')) {
return;
}
// No matter what, the file's base URL needs to be cleared out.
$paths_to_clear = [$file->createFileUrl()];

// If this is an image, we need to clear the edge cache paths for every
// image style, or those won't work.
if (strpos($file->getMimeType(), 'image', 0) === 0) {
$styles = ImageStyle::loadMultiple();
foreach ($styles as $style) {
$file_uri = $file->getFileUri();
$url = $style->buildUrl($file_uri);

$paths_to_clear[] = parse_url($url)['path'];
}
}

try {
pantheon_clear_edge_paths($paths_to_clear);
}
catch (Exception $e) {
\Drupal::logger('pantheon_advanced_page_cache')
->warning('File upload @filename did not successfully clear edge caches. Caches may need to be cleared manually.',
['@filename' => $file->getFileName()]);

\Drupal::logger('pantheon_advanced_page_cache')
->error('@error', ['@error' => $e->getMessage()]);

return;
}

\Drupal::logger('pantheon_advanced_page_cache')
->notice('File paths cleared in edge cache: @paths.',
['@paths' => implode(', ', $paths_to_clear)]);
}

0 comments on commit 9593304

Please sign in to comment.