-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUGS-5816: Resolves issue where changing file content would not serve… (
#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
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]); | ||
} |