From 5e1099f686abcc7026bd4fa74727f3b311c6d6d6 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 11 Sep 2023 15:11:57 +0100 Subject: [PATCH] fix: minify HTML for redirects when emitted as HTML files (#8504) --- .changeset/gold-weeks-hammer.md | 5 +++++ packages/astro/src/core/build/generate.ts | 3 +++ packages/astro/test/redirects.test.js | 6 ++++++ 3 files changed, 14 insertions(+) create mode 100644 .changeset/gold-weeks-hammer.md diff --git a/.changeset/gold-weeks-hammer.md b/.changeset/gold-weeks-hammer.md new file mode 100644 index 000000000..4f6946116 --- /dev/null +++ b/.changeset/gold-weeks-hammer.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Minify the HTML of the redicts emitted during the build. diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 5fe670e99..f1044434b 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -584,6 +584,9 @@ async function generatePath(pathname: string, gopts: GeneratePathOptions, pipeli Redirecting from ${fromPath} to ${location} `; + if (pipeline.getConfig().compressHTML === true) { + body = body.replaceAll('\n', ''); + } // A dynamic redirect, set the location so that integrations know about it. if (pageData.route.type !== 'redirect') { pageData.route.redirect = location; diff --git a/packages/astro/test/redirects.test.js b/packages/astro/test/redirects.test.js index 17c1070dc..999ab1f0f 100644 --- a/packages/astro/test/redirects.test.js +++ b/packages/astro/test/redirects.test.js @@ -69,6 +69,7 @@ describe('Astro.redirect', () => { root: './fixtures/ssr-redirect/', output: 'static', redirects: { + '/old': '/test', '/': '/test', '/one': '/test', '/two': '/test', @@ -82,6 +83,11 @@ describe('Astro.redirect', () => { await fixture.build(); }); + it("Minifies the HTML emitted when a page that doesn't exist is emitted", async () => { + const html = await fixture.readFile('/old/index.html'); + expect(html).to.not.include('\n'); + }); + it('Includes the meta refresh tag in Astro.redirect pages', async () => { const html = await fixture.readFile('/secret/index.html'); expect(html).to.include('http-equiv="refresh');