Skip to content

Commit 314c57a

Browse files
authored
Merge pull request #1915 from hydephp/normalize-asset-cache-busting
[2.x] Normalize asset cache busting
2 parents 82c7600 + d8cb4e4 commit 314c57a

File tree

7 files changed

+34
-27
lines changed

7 files changed

+34
-27
lines changed

config/hyde.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,12 @@
355355
| Cache Busting
356356
|--------------------------------------------------------------------------
357357
|
358-
| Any assets loaded using the Asset::mediaLink() helper will automatically
359-
| have a cache busting query string appended to the URL. This is useful
358+
| Any assets loaded using the Hyde Asset helpers will automatically have
359+
| a "cache busting" query string appended to the URL. This is useful
360360
| when you want to force browsers to load a new version of an asset.
361+
| All included Blade templates use this feature to load assets.
361362
|
362-
| The mediaLink helper is used in the built-in views to load the
363-
| default stylesheets and scripts, and thus use this feature.
364-
|
365-
| To disable cache busting, set this setting to false.
363+
| To disable the cache busting, set this setting to false.
366364
|
367365
*/
368366

packages/framework/config/hyde.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,12 @@
355355
| Cache Busting
356356
|--------------------------------------------------------------------------
357357
|
358-
| Any assets loaded using the Asset::mediaLink() helper will automatically
359-
| have a cache busting query string appended to the URL. This is useful
358+
| Any assets loaded using the Hyde Asset helpers will automatically have
359+
| a "cache busting" query string appended to the URL. This is useful
360360
| when you want to force browsers to load a new version of an asset.
361+
| All included Blade templates use this feature to load assets.
361362
|
362-
| The mediaLink helper is used in the built-in views to load the
363-
| default stylesheets and scripts, and thus use this feature.
364-
|
365-
| To disable cache busting, set this setting to false.
363+
| To disable the cache busting, set this setting to false.
366364
|
367365
*/
368366

packages/framework/src/Facades/Asset.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace Hyde\Facades;
66

7-
use Hyde\Hyde;
87
use Hyde\Support\Filesystem\MediaFile;
98

10-
use function md5_file;
9+
use function hyde;
1110
use function file_exists;
1211

1312
/**
@@ -20,23 +19,16 @@ class Asset
2019
{
2120
public static function get(string $file): string
2221
{
23-
return Hyde::asset($file);
22+
return hyde()->asset($file);
2423
}
2524

2625
public static function mediaLink(string $file): string
2726
{
28-
return Hyde::mediaLink($file).static::getCacheBustKey($file);
27+
return hyde()->mediaLink($file);
2928
}
3029

3130
public static function hasMediaFile(string $file): bool
3231
{
3332
return file_exists(MediaFile::sourcePath($file));
3433
}
35-
36-
protected static function getCacheBustKey(string $file): string
37-
{
38-
return Config::getBool('hyde.enable_cache_busting', true)
39-
? '?v='.md5_file(MediaFile::sourcePath("$file"))
40-
: '';
41-
}
4234
}

packages/framework/src/Foundation/Kernel/Hyperlinks.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use BadMethodCallException;
99
use Hyde\Support\Models\Route;
1010
use Hyde\Foundation\HydeKernel;
11+
use Hyde\Support\Filesystem\MediaFile;
1112
use Hyde\Framework\Exceptions\FileNotFoundException;
1213
use Illuminate\Support\Str;
1314

@@ -99,7 +100,7 @@ public function mediaLink(string $destination, bool $validate = false): string
99100
throw new FileNotFoundException($sourcePath);
100101
}
101102

102-
return $this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination");
103+
return $this->withCacheBusting($this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination"), $destination);
103104
}
104105

105106
/**
@@ -117,10 +118,10 @@ public function asset(string $name): string
117118
$name = Str::start($name, "{$this->kernel->getMediaOutputDirectory()}/");
118119

119120
if ($this->hasSiteUrl()) {
120-
return $this->url($name);
121+
return $this->withCacheBusting($this->url($name), $name);
121122
}
122123

123-
return $this->relativeLink($name);
124+
return $this->withCacheBusting($this->relativeLink($name), $name);
124125
}
125126

126127
/**
@@ -180,4 +181,12 @@ public static function isRemote(string $url): bool
180181
{
181182
return str_starts_with($url, 'http') || str_starts_with($url, '//');
182183
}
184+
185+
/**
186+
* Apply cache to the URL if enabled in the configuration.
187+
*/
188+
protected function withCacheBusting(string $url, string $file): string
189+
{
190+
return $url.MediaFile::getCacheBustKey($file);
191+
}
183192
}

packages/framework/src/Support/Filesystem/MediaFile.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,12 @@ protected static function getMediaGlobPattern(): string
143143
Config::getArray('hyde.media_extensions', self::EXTENSIONS)
144144
));
145145
}
146+
147+
/** @internal */
148+
public static function getCacheBustKey(string $file): string
149+
{
150+
return Config::getBool('hyde.enable_cache_busting', true) && file_exists(MediaFile::sourcePath("$file"))
151+
? '?v='.md5_file(MediaFile::sourcePath("$file"))
152+
: '';
153+
}
146154
}

packages/framework/tests/Feature/Foundation/HyperlinksTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testMediaLinkHelperUsesConfiguredMediaDirectory()
108108
public function testMediaLinkHelperWithValidationAndExistingFile()
109109
{
110110
$this->file('_media/foo');
111-
$this->assertSame('media/foo', $this->class->mediaLink('foo', true));
111+
$this->assertSame('media/foo?v=d41d8cd98f00b204e9800998ecf8427e', $this->class->mediaLink('foo', true));
112112
}
113113

114114
public function testMediaLinkHelperWithValidationAndNonExistingFile()

packages/framework/tests/Feature/Services/RssFeedServiceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public function testXmlChannelDataCanBeCustomized()
8686

8787
public function testMarkdownBlogPostsAreAddedToRssFeedThroughAutodiscovery()
8888
{
89+
config(['hyde.enable_cache_busting' => false]);
90+
8991
file_put_contents(Hyde::path('_posts/rss.md'), <<<'MD'
9092
---
9193
title: RSS

0 commit comments

Comments
 (0)