This module minifies the HTML output of a Silverstripe site using the HtmlMin package.
The current major version supports:
- Silverstripe CMS 6.x
- PHP 8.3+
Silverstripe CMS 4 and 5 are supported by the 1.x release line.
For Silverstripe CMS 6:
composer require dorsetdigital/silverstripe-htmlminifier:^2For Silverstripe CMS 4 or 5:
composer require dorsetdigital/silverstripe-htmlminifier:^1Then flush your Silverstripe configuration cache.
The module is disabled by default. Enable it in your project configuration:
---
Name: minifierconfig
---
DorsetDigital\SSMinifier\Middleware:
enable: true
enable_in_dev: falseThe module configuration options are:
enable- enable HTML minification. Defaults tofalse.enable_in_dev- enable minification while Silverstripe is running in dev mode. Defaults tofalse.options- optional configuration passed to HtmlMin.
If no options are supplied, HtmlMin's own defaults are used.
HtmlMin provides a number of options for controlling the minification process. These can be configured directly through Silverstripe YAML using the method name as the option key.
For example:
DorsetDigital\SSMinifier\Middleware:
enable: true
options:
doRemoveComments: true
doRemoveOmittedQuotes: true
doRemoveOmittedHtmlTags: true
doSortCssClassNames: true
doSortHtmlAttributes: true
doMinifyJavaScript: falseOnly options explicitly supported by this module are passed to HtmlMin. Unsupported or unknown options are ignored.
The following HtmlMin options are supported:
doMakeSameDomainsLinksRelativedoMinifyJavaScriptdoRemoveCommentsdoRemoveDefaultAttributesdoRemoveDeprecatedAnchorNamedoRemoveDeprecatedScriptCharsetAttributedoRemoveDeprecatedTypeFromScriptTagdoRemoveDeprecatedTypeFromStylesheetLinkdoRemoveEmptyAttributesdoRemoveHttpPrefixFromAttributesdoRemoveHttpsPrefixFromAttributesdoRemoveOmittedHtmlTagsdoRemoveOmittedQuotesdoRemoveSpacesBetweenTagsdoRemoveValueFromEmptyInputdoSortCssClassNamesdoSortHtmlAttributesdoSumUpWhitespace
HtmlMin's setLocalDomains option is also supported and accepts an array of domain names. It can be used with doMakeSameDomainsLinksRelative:
DorsetDigital\SSMinifier\Middleware:
enable: true
options:
setLocalDomains:
- example.com
- www.example.com
doMakeSameDomainsLinksRelative: trueThe values are passed directly to HtmlMin, so its defaults and behaviour remain authoritative. See the HtmlMin documentation for details of each option.
- CMS/admin requests are not minified.
- XML sitemap responses are not minified.
- Only HTML and XHTML responses are minified.
- Responses without a Content-Type header are treated as HTML for backwards compatibility.
- JavaScript minification is controlled by HtmlMin and is not enabled by this module unless configured.
Install the development dependencies and run the test suite with:
composer install
composer testThe test suite covers HTML minification and ensures that non-HTML, CMS/admin and sitemap responses are left untouched.
- Uses the excellent HtmlMin package.
- As always, thanks to the Silverstripe core team for all their hard work.