Kirby Html Minifier - Version 0.8


#1

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


Field inherit from parent in extended class
#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.


#3

1. Namespace

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.

2. Variable

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.

Namespaces for dummies

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:


#4

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');

#5

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.


#6

Since you added use Kirby\Component\Response; at the top, change the class name to be just Response.


#7

Thank you, forgot to remove that.

@jenstornell I have corrected it above, does it work now?


#8

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');

Update

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!


#9

Kirby Html Minifier 0.2

New version released.

  • 0.2 - Added namespaces. Inherit response from parent. Added options.
  • 0.1 - Init

#10

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 {

}

#11

You can, but I think it makes sense to show the dependencies of the class clearly.


#12

@jenstornell If you wanna dig deeper into namespaces, this is the tutorial that helped me a while back:


#13

Kirby Html Minifier 0.3

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

  • Changed minifier engine to zaininnari/html-minifier.
  • The issue with breaking tags is fixed with the new minifier engine.
  • Added a fix for package.json.
  • Removed option plugin.html.minifier.options because of the new minifier engine.

Enjoy!

Github https://github.com/jenstornell/kirby-html-minifier


#14

Kirby Html Minifier 0.4

Github: https://github.com/jenstornell/kirby-html-minifier

0.4

  • Changed minifier engine back to mrclay/minify because of minify problems.
  • Made a minor edit to prevent breaking inside tags.
  • Tested SVG files with good result.
  • Option plugin.html.minifier.options added back.
  • Option plugin.html.minifier.blacklist added.

#15

Kirby Html Minifier 0.5

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

  • Wrote an own tiny-html-minifier class to get complete control.
  • Changed to my new minifier class.
  • Option plugin.html.minifier.options is still present, but work a bit different.

#16

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…


#17

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.


#18

ok great, thank you for the reply. Curious to know what the positive effect of html minification is.


#19

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).


#20

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?:smiling_imp: