Skip to content

Commit

Permalink
fix(build): skip only the configured redirects (#10279)
Browse files Browse the repository at this point in the history
* fix(build): allow redirect responses to output files

* add changeset

* add test
  • Loading branch information
lilnasy authored Mar 4, 2024
1 parent 5afc8f2 commit 9ba3e26
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-buttons-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where returning redirect responses resulted in missing files with certain adapters.
5 changes: 3 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,9 @@ async function generatePath(
}

if (response.status >= 300 && response.status < 400) {
// If redirects is set to false, don't output the HTML
if (!config.build.redirects) {
// Adapters may handle redirects themselves, turning off Astro's redirect handling using `config.build.redirects` in the process.
// In that case, we skip rendering static files for the redirect routes.
if (routeIsRedirect(route) && !config.build.redirects) {
return;
}
const locationSite = getRedirectLocationOrThrow(response.headers);
Expand Down
8 changes: 7 additions & 1 deletion packages/astro/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,18 @@ describe('Astro.redirect', () => {
await fixture.build();
});

it('Does not output redirect HTML', async () => {
it('Does not output redirect HTML for redirect routes', async () => {
let oneHtml = undefined;
try {
oneHtml = await fixture.readFile('/one/index.html');
} catch {}
assert.equal(oneHtml, undefined);
});

it('Outputs redirect HTML for user routes that return a redirect response', async () => {
let secretHtml = await fixture.readFile('/secret/index.html');
assert.equal(secretHtml.includes("Redirecting from <code>/secret/</code>"), true);
assert.equal(secretHtml.includes("to <code>/login</code>"), true);
});
});
});

0 comments on commit 9ba3e26

Please sign in to comment.