Skip to content

Commit 48777ec

Browse files
authored
fix: asset cache key with content hash (#2189)
1 parent dfd7ada commit 48777ec

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/Assets/Asset.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function __construct(Builder $builder, string|array $paths, array|null $o
9797
'height' => 0, // image height (in pixels)
9898
'exif' => [], // image exif data
9999
'content' => '', // file content
100+
'hash' => '', // file content hash (md5)
100101
];
101102

102103
// handles options
@@ -142,6 +143,7 @@ public function __construct(Builder $builder, string|array $paths, array|null $o
142143
$this->data['subtype'] = Util\File::getMediaType($file)[1];
143144
$this->data['size'] += filesize($file);
144145
$this->data['content'] .= Util\File::fileGetContents($file);
146+
$this->data['hash'] = hash('md5', $this->data['content']);
145147
// bundle default filename
146148
$filename = $options['filename'];
147149
if ($pathsCount > 1 && empty($filename)) {
@@ -853,6 +855,7 @@ private function getRemoteFileContent(string $path, ?string $userAgent = null):
853855
*/
854856
private function optimize(string $filepath, string $path, int $quality): int
855857
{
858+
$message = \sprintf('Asset not optimized: "%s"', $path);
856859
$sizeBefore = filesize($filepath);
857860
Optimizer::create($quality)->optimize($filepath);
858861
$sizeAfter = filesize($filepath);

src/Assets/Cache.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,25 @@ public function deleteMultiple($keys): bool
174174
}
175175

176176
/**
177-
* Creates key from a string: "$name|uniqid__HASH__VERSION".
177+
* Creates key from a name and a hash: "$name__HASH__VERSION".
178+
*/
179+
public function createKey(string $name, string $hash): string
180+
{
181+
$name = self::sanitizeKey($name);
182+
183+
return \sprintf('%s__%s__%s', $name, $hash, $this->builder->getVersion());
184+
}
185+
186+
/**
187+
* Creates key from a string: "$name__HASH__VERSION".
178188
* $name is optional to add a human readable name to the key.
179189
*/
180-
public function createKey(?string $name, string $value): string
190+
public function createKeyFromValue(?string $name, string $value): string
181191
{
182192
$hash = hash('md5', $value);
183-
$name = $name ? self::sanitizeKey($name) : $hash;
193+
$name = $name ?? $hash;
184194

185-
return \sprintf('%s__%s__%s', $name, $hash, $this->builder->getVersion());
195+
return $this->createKey($name, $hash);
186196
}
187197

188198
/**
@@ -214,7 +224,7 @@ public function createKeyFromAsset(Asset $asset, ?array $tags = null): string
214224
$tagsInline = implode('_', str_replace('_', '', $tags));
215225
$name = "{$asset['_path']}_{$asset['ext']}_$tagsInline";
216226

217-
return $this->createKey($name, $asset['content'] ?? '');
227+
return $this->createKey($name, $asset['hash']);
218228
}
219229

220230
/**
@@ -228,7 +238,7 @@ public function createKeyFromFile(\Symfony\Component\Finder\SplFileInfo $file):
228238
throw new RuntimeException(\sprintf('Can\'t create cache key for "%s".', $file));
229239
}
230240

231-
return $this->createKey($file->getRelativePathname(), $content);
241+
return $this->createKeyFromValue($file->getRelativePathname(), $content);
232242
}
233243

234244
/**

src/Renderer/Extension/Core.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public function minifyCss(?string $value): string
430430
}
431431

432432
$cache = new Cache($this->builder, 'assets');
433-
$cacheKey = $cache->createKey(null, $value);
433+
$cacheKey = $cache->createKeyFromValue(null, $value);
434434
if (!$cache->has($cacheKey)) {
435435
$minifier = new Minify\CSS($value);
436436
$value = $minifier->minify();
@@ -452,7 +452,7 @@ public function minifyJs(?string $value): string
452452
}
453453

454454
$cache = new Cache($this->builder, 'assets');
455-
$cacheKey = $cache->createKey(null, $value);
455+
$cacheKey = $cache->createKeyFromValue(null, $value);
456456
if (!$cache->has($cacheKey)) {
457457
$minifier = new Minify\JS($value);
458458
$value = $minifier->minify();
@@ -472,7 +472,7 @@ public function scssToCss(?string $value): string
472472
$value = $value ?? '';
473473

474474
$cache = new Cache($this->builder, 'assets');
475-
$cacheKey = $cache->createKey(null, $value);
475+
$cacheKey = $cache->createKeyFromValue(null, $value);
476476
if (!$cache->has($cacheKey)) {
477477
$scssPhp = new Compiler();
478478
$outputStyles = ['expanded', 'compressed'];

0 commit comments

Comments
 (0)