Minify HTML, CSS and JavaScript
Minification removes comments and redundant whitespace from HTML, CSS and JavaScript response bodies on the gateway, before they are compressed and sent to the client.
This feature was first introduced in OpenResty Edge 26.12.1-1.
Enable minification
Add the Enable minification action to a page rule (Applications > HTTP Application > Page Rules > Actions), a global rewrite rule, or a global custom action. It has three switches: HTML, CSS and JavaScript. A response is minified when its Content-Type matches an enabled switch:
| Switch | Content types |
|---|---|
| HTML | text/html |
| CSS | text/css |
| JavaScript | application/javascript, text/javascript, application/x-javascript, application/ecmascript, text/ecmascript |
When several rules set the action for one request, the last one wins; setting all three switches off in a later rule disables minification.
What changes for a minified request
- The gateway asks the origin for an uncompressed body, so the origin’s compression is not used for that request. Gateway compression (Gzip, Brotli, Zstandard) still applies afterwards, but only to the content types listed in its settings; add
text/cssand the JavaScript types there when minifying CSS or JavaScript, otherwise those responses reach the client uncompressed. Content-LengthandAccept-Rangesare removed from the response; a strongETagbecomes a weak one;Last-Modifiedis kept.- Byte ranges become unavailable for these URLs. When caching is enabled for the URL, every
Rangerequest, hit or miss, returns the whole minified body with status200and noContent-Range, instead of a206partial response: the gateway does not passRangeon to the origin, and the length of the minified body is not yet known when the range would be applied. When the URL is not cached, theRangerequest reaches the origin; the origin answers206, and that partial response is sent to the client unminified. - Responses that are not minified: bodies the origin compressed anyway,
206,204and304responses, empty bodies,HEADrequests, responses whose charset is not UTF-8, US-ASCII or ISO-8859, and the parts of a response that exceed the minify buffer (see below). - Cached responses are stored unminified and minified on every hit.
Minify buffer size
Global Config > General Settings > Minify > Minify buffer size (default 64 KByte, 8 KByte to 1 MByte). The gateway reserves this much memory per minified response. A single construct larger than the buffer, such as one long inline <script> block, is passed through unminified while the rest of the response is still minified.
Requirements
The gateway node package requires the OpenResty Edge minifier package, so every node running a release that has this feature already has it. A node where it is missing starts normally and ignores the action; its error log then reports that the minifier programs are not loaded. A node picks the package up at its next configuration check, without an operator restarting anything. A node still running an earlier release ignores the action too, and serves those responses unminified until it is upgraded.
Installing, upgrading or removing the package changes the configuration that the node generates for itself, and the node applies that change the same way it applies any other global configuration change: with a binary upgrade of its nginx process at the next configuration check. Client connections are preserved. An upgrade that removes the files before it reinstalls them can trigger two such binary upgrades in a row.
If a minifier file is damaged, truncated or built for a different CPU architecture, the node’s configuration test fails while loading that file. The node keeps serving traffic with its previous configuration and refuses every further global configuration update; the node’s error log names the file that could not be loaded. Reinstall the package on that node, or remove it and install it again; either way the node retries at its next configuration check. Restarting the node is not required.