Modify Kirby's Plugin Assets Route for Cache Busting filenames

Hi guys,

I’m still not that skilled when it comes to OOPHP (I’m still in the learning phase), so I thought I’d ask my question here.

How can I either overwrite or modify the $routes[‘pluginAssets’] route that Kirby ships with, because I have to modify it to work with Cache Busting filenames like /assets/plugins/some-plugin/css/styles.min.1467385194.css where the number is a timestamp from when the file was last modified.

Thank you :blush:

At the moment, I think you can’t unfortunately. Routes you set using kirby()->routes() are overwritten by the built-in ones.

What you could try however: Define your own route that matches the same path as the built-in one and it should be triggered first. Just don’t use the same key, otherwise it will be overwritten. A numeric/no key as in the Kirby routing docs should do.

Thank you @lukasbestle,

I’ve now tried to define the route in a couple of different ways (c::set and new Router registration) but without any luck - do you have a quick example about how I should define the route the way you described it ? :blush:

But I simply don’t get how I really can be the only one who wants to either or both

  1. Cache my resources
  2. Be able to purge em so that people isn’t potentially left with broken caches

What are you guys doing? :blush: Not using Plugin Assets? Not caching at all? Semi-cache for 30 days and expects no one would ever notice potentially broken assets, and guides your clients to purge their browser cache all the time? I simply don’t get how this adds up :confounded:

Something like this should work in theory, but I haven’t tested this:

kirby()->routes(array(
  array(
    'pattern' => 'assets/plugins/(:any)/(:all)\.(:num)\.(:any)',
    'method'  => 'GET',
    'action'  => function($plugin, $path, $timestamp, $extension) {
      $root = kirby()->roots()->plugins() . DS . $plugin . DS . 'assets' . DS . $path . '.' . $extension;
      $file = new Media($root);

      if($file->exists()) {
        return new Response(f::read($root), f::extension($root));
      } else {          
        return new Response('The file could not be found', f::extension($path), 404);
      }
    }
)));

I guess not using a combination of plugin assets and files that change often. I have to admit that I personally haven’t used plugin assets so far, but you are right that cache busting would be great here. See this issue on GitHub.

I’m also trying to find a good solution for this. Doing some research right now…