Skip to content

Commit

Permalink
fix: minify HTML for redirects when emitted as HTML files (#8504)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Sep 11, 2023
1 parent c934b08 commit 5e1099f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-weeks-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Minify the HTML of the redicts emitted during the build.
3 changes: 3 additions & 0 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ async function generatePath(pathname: string, gopts: GeneratePathOptions, pipeli
<body>
<a href="${location}">Redirecting from <code>${fromPath}</code> to <code>${location}</code></a>
</body>`;
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;
Expand Down
6 changes: 6 additions & 0 deletions packages/astro/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Astro.redirect', () => {
root: './fixtures/ssr-redirect/',
output: 'static',
redirects: {
'/old': '/test',
'/': '/test',
'/one': '/test',
'/two': '/test',
Expand All @@ -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');
Expand Down

0 comments on commit 5e1099f

Please sign in to comment.