Fix Astro 7 compressHTML Spacing Bug After Upgrade

The same strong and span markup rendered under two Astro versions: Astro 6 shows 'Save 20%' with the space intact, Astro 7 shows 'Save20%' with the space collapsed by the new compressHTML default
On this page

Why Astro 7 collapses this space

Astro 7 changes compressHTML’s default value to 'jsx'. The official Astro v7 upgrade guide explains the new rule: whitespace and line breaks around elements are stripped the same way JSX frameworks like React strip them, replacing Astro 6’s own HTML-aware compression. A real space typed on the same line survives. A line break with nothing else on it does not.

Verified directly on this site’s own astro@7.1.3 install, which doesn’t override compressHTML in astro.config.mjs and so runs on the new default: two inline elements written on separate lines compile to <strong>5</strong><span>posts</span>, no space at all, while the same markup with a space typed on one line compiles to <strong>5</strong> <span>posts</span>.

Two expressions merging instead? Same cause, different post

If your two pieces are {expression} blocks rather than real HTML tags, like {count} and {label}, you’re hitting the same compressHTML default flip, just on a different pair of node types. Astro 6’s compressHTML: true preserved that spacing too, confirmed by testing both cases against this repo’s own astro.config.mjs. See why Astro silently merges text like ‘5posts’ into one word for the expression-specific fix.

Fix it: add the explicit space back

Before: renders fine in Astro 6, broken in Astro 7

two inline elements on separate lines
<p>
  <strong>5</strong>
  <span>posts</span>
</p>

Compiles to, confirmed on astro@7.1.3:

Astro 7 output, space gone
<p><strong>5</strong><span>posts</span></p>

After: explicit space expression

fixed: explicit space between elements
<p>
  <strong>5</strong>{" "}
  <span>posts</span>
</p>

Compiles to, confirmed on the same install:

fixed output, space restored
<p><strong>5</strong> <span>posts</span></p>

{" "} is a string literal, not whitespace-only text, so Astro renders it exactly as written no matter how the surrounding lines wrap.

Restore Astro 6’s behavior sitewide

If this shows up in many places at once, patching every instance is slower than restoring the old default in one place.

astro.config.mjs
export default defineConfig({
  compressHTML: true,
  // ...rest of your config
});

This brings back Astro 6’s HTML-aware compression everywhere. It’s still real compression, so genuinely meaningless whitespace still gets removed. It just stops applying the JSX rule that strips a line break sitting between two elements. This option is documented directly in Astro’s own upgrade guide; the explicit-space fix above is the one independently confirmed against this site’s build.

Check every place your markup pairs two inline elements across separate lines after upgrading, not just the one spot where someone already noticed. It costs nothing to look, and the mistake stays invisible until a reader points it out. Browse more posts like this in the Guides & Fixes archive.

Frequently asked

Does this affect every space between elements, or only some?

Only whitespace-only text made of nothing but a line break and indentation. A real space typed on the same line survives, and so does any real static text sitting between two elements. Only a line break with nothing else on it gets stripped.

Is this the same bug as Astro merging {count} and {label} into one word?

Same cause, different code shape. Both come from compressHTML's default flip to 'jsx' in Astro 7; setting compressHTML: true restores Astro 6's spacing for both. This post's version happens between real HTML elements like <strong> and <span>. The {count}{label} version happens between two expressions with nothing else between them. Both are fixed the same way: an explicit space expression where the line break used to be.

Emitted as FAQPage JSON-LD from the same frontmatter — one source, no duplicated prose.

Recent posts

Full-text search via Pagefind · ↑↓ to navigate · ↵ to open