schnti / kirby3-cachebuster

11 stars 4 forks source link

Breaks external urls #5

Open rasteiner opened 5 years ago

rasteiner commented 5 years ago

Hello, nice plugin!

I sometimes use the css and js helpers to load external urls; like from public CDNs. The plugin doesn't distinguish between local and remote urls, therefore it tries to add modification timestamps also to external urls (which fails, and also if it would succeed, the url would then probably return a 404).

Quick and dirty fix:

Ignore the urls that specify a "host" part.


Kirby::plugin('schnti/cachebuster', [
    'options'    => [
        'active' => true
    ],
    'components' => [
        'css' => function ($kirby, $url) {
-           if ($kirby->option('schnti.cachebuster.active')) {
+           if (!isset(parse_url($url)['host']) &&  $kirby->option('schnti.cachebuster.active')) {

                $file = $kirby->roots()->index() . DS . $url;
                return dirname($url) . '/' . F::name($url) . '.' . F::modified($file) . '.css';

            } else {
                return $url;
            }
        },
        'js'  => function ($kirby, $url) {
-           if ($kirby->option('schnti.cachebuster.active')) {
+           if (!isset(parse_url($url)['host']) && $kirby->option('schnti.cachebuster.active')) {

                $file = $kirby->roots()->index() . DS . $url;
                return dirname($url) . '/' . F::name($url) . '.' . F::modified($file) . '.js';

            } else {
                return $url;
            }
        }
    ]
]);
apps4research commented 11 months ago

Thanks for this however, it breaks the url of any css or js files in the templates folder.

rasteiner commented 11 months ago

Thanks for this however, it breaks the url of any css or js files in the templates folder.

yes, though it looks like @auto urls didn't work with the original version either.

For myself I'm now using a fork of this plugin: https://github.com/rasteiner/kirby3-cachebuster which combines suggestions from carstenjaksch and adds the F::exists check to it. Should work with external urls and @auto. At least it seems to do so for me :)

The updated file is here: https://github.com/rasteiner/kirby3-cachebuster/blob/master/index.php

apps4research commented 11 months ago

Thanks rasteiner, much appreciated :)