Menu

The minifier that ate your spaces: why HTML minifying is harder than it looks
πŸ“°

The minifier that ate your spaces: why HTML minifying is harder than it looks

DEV Community: htmlΒ·Mubeen KhanΒ·about 23 hours ago
#eo18mX7O
#devto2#devto#dev#html#minifier
Reading 0:00
15s threshold

Minifying CSS is easy. Minifying JavaScript is a solved problem. Minifying HTML looks easy too, until the day your minifier eats a space that mattered and two words glue themselves together on the live page.

Here is the trap. In HTML, whitespace is not decoration. It is content.

The space between two words is load-bearing

Take this markup:

<p> Click <a href = "/pricing" > here </a> to see the price. </p>

Enter fullscreen mode

Exit fullscreen mode

There is a space after "Click" and a space before "to". A careless minifier sees whitespace between tags and deletes it, because most whitespace in HTML is meaningless. But these two spaces are the only thing keeping "Click", "here" and "to see the price" as separate words. Strip them and your page reads "Clickhere to see the price." or worse, "Clickhereto see the price."

The same rule applies everywhere inline elements touch text: <strong> , <em> , <span> , <a> , <code> . A minifier that treats HTML like a flat string will break your sentences.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.