Skip to content
10 changes: 4 additions & 6 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,12 @@
| Cache Busting
|--------------------------------------------------------------------------
|
| Any assets loaded using the Asset::mediaLink() helper will automatically
| have a cache busting query string appended to the URL. This is useful
| Any assets loaded using the Hyde Asset helpers will automatically have
| a "cache busting" query string appended to the URL. This is useful
| when you want to force browsers to load a new version of an asset.
| All included Blade templates use this feature to load assets.
|
| The mediaLink helper is used in the built-in views to load the
| default stylesheets and scripts, and thus use this feature.
|
| To disable cache busting, set this setting to false.
| To disable the cache busting, set this setting to false.
|
*/

Expand Down
10 changes: 4 additions & 6 deletions packages/framework/config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,12 @@
| Cache Busting
|--------------------------------------------------------------------------
|
| Any assets loaded using the Asset::mediaLink() helper will automatically
| have a cache busting query string appended to the URL. This is useful
| Any assets loaded using the Hyde Asset helpers will automatically have
| a "cache busting" query string appended to the URL. This is useful
| when you want to force browsers to load a new version of an asset.
| All included Blade templates use this feature to load assets.
|
| The mediaLink helper is used in the built-in views to load the
| default stylesheets and scripts, and thus use this feature.
|
| To disable cache busting, set this setting to false.
| To disable the cache busting, set this setting to false.
|
*/

Expand Down
14 changes: 3 additions & 11 deletions packages/framework/src/Facades/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Hyde\Facades;

use Hyde\Hyde;
use Hyde\Support\Filesystem\MediaFile;

use function md5_file;
use function hyde;
use function file_exists;

/**
Expand All @@ -20,23 +19,16 @@ class Asset
{
public static function get(string $file): string
{
return Hyde::asset($file);
return hyde()->asset($file);
}

public static function mediaLink(string $file): string
{
return Hyde::mediaLink($file).static::getCacheBustKey($file);
return hyde()->mediaLink($file);
}

public static function hasMediaFile(string $file): bool
{
return file_exists(MediaFile::sourcePath($file));
}

protected static function getCacheBustKey(string $file): string
{
return Config::getBool('hyde.enable_cache_busting', true)
? '?v='.md5_file(MediaFile::sourcePath("$file"))
: '';
}
}
15 changes: 12 additions & 3 deletions packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use BadMethodCallException;
use Hyde\Support\Models\Route;
use Hyde\Foundation\HydeKernel;
use Hyde\Support\Filesystem\MediaFile;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Illuminate\Support\Str;

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

return $this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination");
return $this->withCacheBusting($this->relativeLink("{$this->kernel->getMediaOutputDirectory()}/$destination"), $destination);
}

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

if ($this->hasSiteUrl()) {
return $this->url($name);
return $this->withCacheBusting($this->url($name), $name);
}

return $this->relativeLink($name);
return $this->withCacheBusting($this->relativeLink($name), $name);
}

/**
Expand Down Expand Up @@ -180,4 +181,12 @@ public static function isRemote(string $url): bool
{
return str_starts_with($url, 'http') || str_starts_with($url, '//');
}

/**
* Apply cache to the URL if enabled in the configuration.
*/
protected function withCacheBusting(string $url, string $file): string
{
return $url.MediaFile::getCacheBustKey($file);
}
}
8 changes: 8 additions & 0 deletions packages/framework/src/Support/Filesystem/MediaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,12 @@ protected static function getMediaGlobPattern(): string
Config::getArray('hyde.media_extensions', self::EXTENSIONS)
));
}

/** @internal */
public static function getCacheBustKey(string $file): string
{
return Config::getBool('hyde.enable_cache_busting', true) && file_exists(MediaFile::sourcePath("$file"))
? '?v='.md5_file(MediaFile::sourcePath("$file"))
: '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testMediaLinkHelperUsesConfiguredMediaDirectory()
public function testMediaLinkHelperWithValidationAndExistingFile()
{
$this->file('_media/foo');
$this->assertSame('media/foo', $this->class->mediaLink('foo', true));
$this->assertSame('media/foo?v=d41d8cd98f00b204e9800998ecf8427e', $this->class->mediaLink('foo', true));
}

public function testMediaLinkHelperWithValidationAndNonExistingFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public function testXmlChannelDataCanBeCustomized()

public function testMarkdownBlogPostsAreAddedToRssFeedThroughAutodiscovery()
{
config(['hyde.enable_cache_busting' => false]);

file_put_contents(Hyde::path('_posts/rss.md'), <<<'MD'
---
title: RSS
Expand Down