Skip to content

Breaks external urls #5

Open
Open
@rasteiner

Description

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

Activity

apps4research

apps4research commented on Sep 26, 2023

@apps4research

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

rasteiner

rasteiner commented on Sep 26, 2023

@rasteiner
Author

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

apps4research commented on Sep 26, 2023

@apps4research

Thanks rasteiner, much appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Breaks external urls · Issue #5 · schnti/kirby3-cachebuster