HTML minify for K3?

What about HTML minification, may this even be enabled by default? :slight_smile:

BTW love the new srcset feature, great stuff people!
thx!

3 Likes

looking also forward to html minification. maybe configurable in the config, like the old krb plugin for k2

Any news about HTML minification?

Not sure about html minification because they always broke everything for me.

As for js css and the above krb plugin, in k3 I am replacing krb with this one here:

i agree with @carstengrimm. html minification is hardly worth the trouble.

i try to make sure i use semantic html (no useless “div-ing”) and servers response html is send gziped. otherwise its mostly images, videos and font files that slow down the initial load.
there are some very good threads on web performance in general not just with kirby hidden in this forum but the search will help you find these in no time.

I made a very simple plugin.
May I have your reviews?

2 Likes

It needs an option that can sit inside the config to turn it on and off.

'minifyhtml' => true/false

I like the simplicity of this approach, @ahmetbora.
Have you tested this with <pre> elements which need their whitespace to work correctly?

Hmm, not tested but it won’t work.
I guess, i can implement an option skip for tags.
And enable disable option like @kinkwilde said.

Just found this which might be interesting:

@ahmetbora might be easier to lean on a mature library meant for this. I hope you dont mind but I tweaked your plugin a little by adding voku/HtmlMin. Gets the file minified and it plays nice with <pre> and <code> tags.

Add the on / off config option and the job is done :slight_smile:

<?php

@include_once __DIR__ . '/vendor/autoload.php';

use Kirby\Toolkit\Tpl;
use Kirby\Cms\Template;
use Kirby\Cms\App as Kirby;

use voku\helper\HtmlMin;


class MinifyHTML extends Template
{
    public function render(array $data = []): string
    {

        $html = Tpl::load($this->file(), $data);

        $htmlMin = new HtmlMin();

        return $htmlMin->doSortCssClassNames()->doSortHtmlAttributes()->minify($html);
    }
}

Kirby::plugin('afbora/kirby-minify-html', [
    'components' => [
        'template' => function (Kirby $kirby, string $name, string $contentType = null) {
            return new MinifyHTML($name, $contentType);
        }
    ]
]);

1 Like

You’re amazing :clap:
Implemented that library with new options.
Your reviews will please me.

2 Likes

Awesome… testing it out now. First issue… composer install does not work :slight_smile:

With submodule?

Fixed typo on installing submodule:

git submodule add https://github.com/afbora/kirby-minify-html.git site/plugins/kirby-minify-html

I don’t use submodules. I think the issue is that it hasn’t been added to packagist.

Yes I didn’t, I just added :+1:

1 Like

the k2 html minify plugin by @jenstornell had a blacklist to exclude pages based on uid. maybe you could add something similar? i would suggest using a plain callback like the core pages cache ignore option does.

using semver like 1.1.0 also helps keeping the plugin packagist friendly.

Feel free to take inspiration from my repo.

Works great, thanks for this :slight_smile: