Inspired by Kirby MinifyHTML by @iancox.
This plugin uses the response
component. That means no output buffer is needed. Awesome!
Require Kirby 2.3 beta 2
Inspired by Kirby MinifyHTML by @iancox.
This plugin uses the response
component. That means no output buffer is needed. Awesome!
Require Kirby 2.3 beta 2
That’s a great idea, but you could even make it simpler to be future-proof in case the core component changes:
class HtmlMinifier extends Kirby\Component\Response {
public function make($response) {
$buffer = parent::make($response);
if( $buffer && c::get('plugin.html.minifier.active', true) ) {
return Minify_HTML::minify( $buffer );
}
return $buffer;
}
}
Also it is probably a good idea to namespace your class to avoid collision with other plugins or core classes.
You can for example use JensTornell\HtmlMinifier
.
The parent::make
thing works great but the namespace does not. When creating a dummy class it works, but not for an extended object.
In a new file core.php
:
<?php
namespace JensTornell\HtmlMinifier;
class core extends Kirby\Component\Response {
public function make($response) {
$buffer = parent::make( $response );
if( $buffer && c::get('plugin.html.minifier.active', true) ) {
return Minify_HTML::minify( $buffer );
}
return $buffer;
}
}
Fatal error: Class ‘JensTornell\HtmlMinifier\Kirby\Component\Response’ not found
It seems to namespace the component as well.
Even if it’s possible to make the above work somehow, I don’t know what to input in the third parameter…
$kirby->set('component', 'response', 'HtmlMinifier');
Maybe this:
$kirby->set('component', 'response', '\JensTornell\HtmlMinifier\core);
Or this:
use \JensTornell\HtmlMinifier\core as Core;
$kirby->set('component', 'response', 'Core');
If I can get these two to work, I’ll namespace all my plugins.
I did not find any articles simple enough to explain how to work with namespaces, but then I found this and now I can do the very basics of it.
For all you dummies out there, this 120 sec video is nice:
It has to be this:
<?php
namespace JensTornell;
use Kirby\Component\Response;
use Minify_HTML;
include __DIR__ . DS . 'minify' . DS . 'HTML.php';
class HtmlModifier extends Response {
public function make($response) {
$buffer = parent::make( $response );
if( $buffer && c::get('plugin.html.minifier.active', true) ) {
return Minify_HTML::minify( $buffer );
}
return $buffer;
}
}
$kirby->set('component', 'response', 'JensTornell\HtmlMinifier');
Maybe almost there but still errors. At least it looks nice.
Fatal error: Class ‘JensTornell\Kirby\Component\Response’ not found in C:\wamp\www\minify-html-evolution\site\plugins\html-minifier\html-minifier.php on line 9
To be sure of it I just copy pasted your code and tried it.
Since you added use Kirby\Component\Response;
at the top, change the class name to be just Response
.
Thank you, forgot to remove that.
@jenstornell I have corrected it above, does it work now?
Thanks @pedroborges and @lukasbestle for your help. I think that solved that issue.
Too bad a new issue appear. It has to do with the last line of the code.
Fatal error: Class ‘JensTornell\HtmlMinifier’ not found in C:\wamp\www\minify-html-evolution\kirby\kirby.php on line 762
Something is wrong with this one:
$kirby->set('component', 'response', 'JensTornell\HtmlMinifier');
I think I found it. Change class name from:
class HtmlModifier extends Response {
to…
class HtmlMinifier extends Response {
And include this line somewhere at the top:
use c;
Thanks guys!
New version released.
Note that when you’re using a namespace and want to use a class from another namespace (or a global one), you don’t have to use use Foo\Bar;
. You can just call \Foo\Bar
.
namespace My\Cool\Stuff;
class MyResponseComponent extends \Kirby\Component\Response {
}
You can, but I think it makes sense to show the dependencies of the class clearly.
@jenstornell If you wanna dig deeper into namespaces, this is the tutorial that helped me a while back:
I was about to close this plugin because of other plugins doing the same thing and they seemed to do it better. I had some issues with them and I still think my plugin is the best one (personal opinion). Therefor I decided to update it instead.
0.3
plugin.html.minifier.options
because of the new minifier engine.Enjoy!
Github: https://github.com/jenstornell/kirby-html-minifier
0.4
plugin.html.minifier.options
added back.plugin.html.minifier.blacklist
added.Github: https://github.com/jenstornell/kirby-html-minifier
As I got tired of html minifiers that was not working, too bloated or not for PHP, I decided to write my own html minifier, which I did. I think it’s easier to understand for most people because it don’t have that many regular expressions, it’s just a single file and there are no dependencies like composer to install it.
The new Kirby Html Minifier has therefor changed minifier engine yet again, hopefully for the last time.
0.5
plugin.html.minifier.options
is still present, but work a bit different.Thanks for the this plugin. Sorry if this is a basic question, but does this plugin process the html on page load or is it done on panel save? In the past I have used html minifers that actually slow down page loads…
The minification is done on page load but in my tests the minification was really really fast like 0.007 seconds or something.
If it would be done on save, the panel would be a bit slower when saving and the biggest problem would be that it would not know what pages would be affected by the page. An archive could contain information from 10 different pages.
If you still worry about load time you could combine the minify plugin with Kirby cache.
ok great, thank you for the reply. Curious to know what the positive effect of html minification is.
Less bandwidth for the user, faster loadtime (especially for mobile devices) and because Google like minified html, you will gain some SEO points as well (you could rank higher in the Google search results).
Only if the rest of your site is already optimized, as a last step, I’d say. Otherwise, the effect is probably negligible. Also depends on the amount of (superfluous) HTML code in your pages. The effect of not using a CSS framework like Bootstrap, for example, is probably higher than minifying HTML?