byrnereese/mkdocs-minify-plugin

Template pages aren't minified, e.g., 404.html

wilhelmer opened this issue · 4 comments

When using HTML minification, the 404.html file isn't minified.

This is because the plugin uses the on_post_page event for minification, and that event isn't fired for the 404 page.
@waylan Is this intended, because the 404 page is considered as some kind of "built-in" page, i.e., a different kind of page?

As a workaround, we can use on_post_build and minify 404.html manually after it has been placed in the output directory. Because the filename and path of the 404 page are fixed (site_dir/404.html), this should work, even though it's probably not the most elegant solution.

I will submit a PR with the proposed solution.

All of the "page" events are only fired for Markdown pages. The error page is one of the static_templates. Static templates can be provided by the user or the theme. As there is no Markdown processing, they are processed differently and do not fire the "page" events. This is the intended behavior.

If you want to minify static templates, then you cannot use page events. IIRC, all static templates (including error pages) are listed in the static_templates config setting. You should be able to loop through that list and act on each one.

I almost forgot, you want a template event.

Template events are called once for each non-page template. Each template event will be called for each template defined in the extra_templates config setting as well as any static_templates defined in the theme. All template events are called after the env event and before any page events.

Notice that they "are called once for each non-page template," which is what you want. In other words, they will fire only on pages which do not get the "page events". If you want to do an operation on all HTML output by a build, then you would need to run that operation on both the on_post_page and on_post_template events. Although you do need to be careful to check that the output of the template event is HTML. Some of the templates may be for other types of content (XML for example).

I almost forgot, you want a template event.

Uuh, that looks promising. I'll look at that tomorrow and update the PR if it's working as expected.

Thanks a lot!

PR updated, this is much better. Thanks again for the help.