Hi,
I recently started a new blog using Kirby. You can look at it at https://emanuelpina.pt
Today, I’m asking for help to implement a CDN solution for my use case.
And what’s my use case?
I use Bunny as CDN. I don’t want to use their image optimization service. So all I want is that my assets and media files are served by the CDN.
So, instead of emanuelpina.pt/assets/my-asset.css
I want Kirby to look for cdn.emanuelpina.pt/assets/my-asset.css
Instead of emanuelpina.pt/media/my-image.jpg
I want Kirby to look for cdn.emanuelpina.pt/media/my-image.jpg
And instead of emanuelpina.pt/media/my-image-100x100-crop-right-q90.jpg
I want Kirby to look for cdn.emanuelpina.pt/assets/my-100x100-crop-right-q90.jpg
Following the Kirby loves CDN recipe I was able to achieve what I wanted for css and svg files on assets folder, but not for images or other files.
The above recipe was made to be used with KeyCDN image process service and as I understand I need to implement the file::version
and file::url
differently of what’s in the recipe. The problem is that I can’t figure how. Can someone help me finding the solution to my use case?
My current plugins\cdn\index.php
:
<?php
load([
'kirby\\cdn\\cachebuster' => __DIR__ . '/src/Cachebuster.php'
]);
use Kirby\Cdn\Cachebuster;
Kirby::plugin('emanuelpina/cdn', [
'components' => [
'url' => function ($kirby, $path, $options) {
if (Str::startsWith($path, 'assets')) {
$path = Cachebuster::path($path);
if (option('cdn', false) !== false) {
return option('cdn.domain') . '/' . $path;
}
}
$original = $kirby->nativeComponent('url');
return $original($kirby, $path, $options);
}
]
]);
Thanks