Skip to content

Breaks external urls #5

@rasteiner

Description

@rasteiner

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;
			}
		}
	]
]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions