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
… new files.

Contributors:
* [nsciacca](https://git.drupalcode.org/nsciacca)
* [Ec1ipsis](https://git.drupalcode.org/Ec1ipsis)
  • Loading branch information
Ryan Wagner committed Mar 15, 2024
1 parent 96d5b49 commit fa9e3bc
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pantheon_advanced_page_cache.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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')) {
// 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'];
}
}

pantheon_clear_edge_paths($paths_to_clear);
}
}

0 comments on commit fa9e3bc

Please sign in to comment.