-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpantheon_advanced_page_cache.module
49 lines (41 loc) · 1.65 KB
/
pantheon_advanced_page_cache.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 ((class_exists(ImageStyle::class)) && (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)]);
}