Skip to main content

Minify

Mutate Content Optimization

Synopsis

A content optimization processor that minifies XML, JSON, and HTML documents by removing unnecessary whitespace, comments, and other non-essential elements to reduce file size and improve performance.

Schema

- minify:
field: <ident>
format: <enum>
target_field: <ident>
keep_whitespace: <boolean>
keep_comments: <boolean>
description: <text>
if: <script>
ignore_failure: <boolean>
ignore_missing: <boolean>
keep_end_tags: <boolean>
keep_document_tags: <boolean>
on_failure: <processor[]>
on_success: <processor[]>
tag: <string>

Configuration

The following fields are used to define the processor:

FieldRequiredDefaultDescription
fieldY-Field containing the content to minify
formatY-Content format: xml, json, or html. Any other value fails the processor
target_fieldNfieldField to store the minified content
keep_whitespaceNfalsePreserve whitespace. XML and HTML only; JSON ignores it
keep_commentsNfalsePreserve comments. HTML only — it also covers conditional comments. XML and JSON ignore it, and XML comments are always stripped
keep_end_tagsNfalsePreserve end tags in HTML (HTML only)
keep_document_tagsNfalsePreserve document tags in HTML (HTML only)
descriptionN-Explanatory note
ifN-Condition to run
ignore_failureNfalseContinue if minification fails
ignore_missingNfalseContinue if source field doesn't exist
on_failureN-See Handling Failures
on_successN-See Handling Success
tagN-Identifier
disabledNfalseWhen true, the processor is skipped and the event continues to the next one. Lets you take a processor out of the path without removing its configuration

Details

The processor provides content minification for three primary formats: XML, JSON, and HTML. Each format has specific optimization strategies tailored to its structure and common use cases.

note

Minification is irreversible - ensure you preserve original content if needed.

XML minification removes whitespace between elements and always strips commentskeep_comments is not wired to it. JSON is compacted with the standard Go encoder, which preserves numeric types and avoids scientific notation, and takes no options at all. HTML is the only format that honours the full option set.

keep_end_tags, keep_document_tags and keep_comments reach the HTML minifier only. keep_whitespace reaches XML and HTML. Setting an option on a format that ignores it is silent, not an error.

warning

Test minified content thoroughly as some applications may depend on specific formatting.

Examples

XML Minification

Minifying XML content...

{
"xml_data": "<?xml version=\"1.0\"?>\n<root>\n <item>value</item>\n <!-- comment -->\n</root>"
}
- minify:
field: xml_data
format: xml
target_field: minified_xml

produces compact XML:

{
"xml_data": "<?xml version=\"1.0\"?>\n<root>\n <item>value</item>\n <!-- comment -->\n</root>",
"minified_xml": "<?xml version=\"1.0\"?><root><item>value</item></root>"
}

JSON Minification

Compacting JSON data...

{
"json_content": "{\n \"name\": \"example\",\n \"value\": 123,\n \"nested\": {\n \"key\": \"data\"\n }\n}"
}
- minify:
field: json_content
format: json

removes all formatting:

{
"json_content": "{\"name\":\"example\",\"value\":123,\"nested\":{\"key\":\"data\"}}"
}

HTML Minification

Optimizing HTML content...

{
"html_page": "<!DOCTYPE html>\n<html>\n <head>\n <title>Example</title>\n </head>\n <body>\n <!-- Main content -->\n <div class=\"container\">\n <p>Hello World</p>\n </div>\n </body>\n</html>"
}
- minify:
field: html_page
format: html
target_field: optimized_html

creates compact HTML:

{
"html_page": "<!DOCTYPE html>\n<html>\n <head>\n <title>Example</title>\n </head>\n <body>\n <!-- Main content -->\n <div class=\"container\">\n <p>Hello World</p>\n </div>\n </body>\n</html>",
"optimized_html": "<!doctype html><html><head><title>Example</title><body><div class=container><p>Hello World"
}

Preserving Comments in HTML

keep_comments works for HTML only...

{
"page": "<html>\n <!-- Important setting -->\n <body>value</body>\n</html>"
}
- minify:
field: page
format: html
keep_comments: true
keep_whitespace: false

keeps the comment while removing the layout whitespace — the same configuration with format: xml would strip it:

{
"page": "<html><!-- Important setting --><body>value</body></html>"
}

Conditional Minification

Minifying based on content size...

{
"large_json": "{\n \"data\": \"large content here\"\n}",
"content_size": 1024
}
- minify:
field: large_json
format: json
if: "content_size > 500"
target_field: compressed_json

applies minification when needed:

{
"large_json": "{\n \"data\": \"large content here\"\n}",
"content_size": 1024,
"compressed_json": "{\"data\":\"large content here\"}"
}

HTML with Preserved Structure

Minifying HTML while keeping structure...

{
"template": "<html>\n<head></head>\n<body>\n <main>Content</main>\n</body>\n</html>"
}
- minify:
field: template
format: html
keep_end_tags: true
keep_document_tags: true
target_field: clean_template

maintains essential HTML structure:

{
"template": "<html>\n<head></head>\n<body>\n <main>Content</main>\n</body>\n</html>",
"clean_template": "<html><head></head><body><main>Content</main></body></html>"
}