I want to cache my generated javascript / or even css files, which are dynamically created depending on what snippets I include (e.g. one pager with different sections)
the optimal way would be using
timestamp / version (for cache refresh)
delete old files after page refresh
is there any way i can leverage the existing caching functions where these could be purged after changing though the panel?
function cacheScripts(){
$page = page();
$file = 'assets/js/pages/'.$page->uid().'.js';
$asset = new Asset($file);
$asset->write(snippet('landing-scripts',[],true));
return js($file);
}
(in this the generation is already cached with the cache driver after the page loaded for the first time)
In this example code, itād generate a static script file which can then be included e.g.
ā¦/home.js
ā¦/about-us.js
etc.pp.
ofc adding a modified handle would solve the āin cacheā problem, however this would generate many files over time which are not deleted after changing a page.
Ofc it would be possible to add Clearing out the Directory first, before the generation happens, i just wondered if thereās any build in tools available within the caching functions, to automatically do it.
but if i could solve that with a simple and straight-forward approach, i would skip using third party plugins ⦠once in a while i encounter issues with plugins breaking after getting a newer update release.
canāt seem to get it working (installed with composer)
I adjusted my example code, which would do the job in case of file-cached driver ⦠So after a change via panel on the next load of the content page, itās clearing out the uid folder then saving the latest version as timestamp.js
function cacheScripts(){
$page = page();
$folder = 'assets/js/pages/'.$page->uid();
$dir = Dir::read($folder);
foreach($dir as $file){
$asset = new Asset($folder.DS.$file);
$asset->delete();
}
$file = $folder.DS.$page->modified().'.js';
$asset = new Asset($file);
$asset->write(snippet('landing-scripts',[],true));
return js($asset->url(),true);
}
// in Code / Template Bottom
<?= cacheScripts() ?>
// output == home/timestamp.js
// <script async src="https://dev.test/assets/js/pages/home/1680174232.js"></script>