Skip to content

Commit 5b4474e

Browse files
committed
Merge branch 'normalize-the-asset-api' into normalize-asset-helpers
2 parents f82e148 + 314c57a commit 5b4474e

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 Illuminate\Support\Str;
1213

1314
use function str_ends_with;
@@ -92,7 +93,7 @@ public function relativeLink(string $destination): string
9293
*/
9394
public function mediaLink(string $destination): string
9495
{
95-
return $this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination");
96+
return $this->withCacheBusting($this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination"));
9697
}
9798

9899
/**
@@ -110,10 +111,10 @@ public function asset(string $name): string
110111
$name = Str::start($name, "{$this->kernel->getMediaOutputDirectory()}/");
111112

112113
if ($this->hasSiteUrl()) {
113-
return $this->url($name);
114+
return $this->withCacheBusting($this->url($name), $name);
114115
}
115116

116-
return $this->relativeLink($name);
117+
return $this->withCacheBusting($this->relativeLink($name), $name);
117118
}
118119

119120
/**
@@ -173,4 +174,12 @@ public static function isRemote(string $url): bool
173174
{
174175
return str_starts_with($url, 'http') || str_starts_with($url, '//');
175176
}
177+
178+
/**
179+
* Apply cache to the URL if enabled in the configuration.
180+
*/
181+
protected function withCacheBusting(string $url, string $file): string
182+
{
183+
return $url.MediaFile::getCacheBustKey($file);
184+
}
176185
}

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
@@ -107,7 +107,7 @@ public function testMediaLinkHelperUsesConfiguredMediaDirectory()
107107
public function testMediaLinkHelperWithExistingFile()
108108
{
109109
$this->file('_media/foo');
110-
$this->assertSame('media/foo', $this->class->mediaLink('foo'));
110+
$this->assertSame('media/foo?v=d41d8cd98f00b204e9800998ecf8427e', $this->class->mediaLink('foo', true));
111111
}
112112

113113
public function testMediaLinkHelperWithNonExistingFile()

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)