Skip to content

Commit 1cd8c3b

Browse files
qw-inematipico
andauthored
fix: isPrerendered now set correctly in static configured redirects. (#13924)
* chore: add test for isPrerendered in static redirects * fix: isPrerendered now set correctly in static configured redirects. * Update .changeset/sharp-vans-greet.md --------- Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent 4d96bda commit 1cd8c3b

File tree

8 files changed

+56
-1
lines changed

8 files changed

+56
-1
lines changed

.changeset/sharp-vans-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes an edge case where `isPrerendered` was incorrectly set to `false` for static redirects.

packages/astro/src/core/routing/manifest/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ function createRedirectRoutes(
376376
component: from,
377377
generate,
378378
pathname: pathname || void 0,
379-
prerender: false,
379+
prerender: getPrerenderDefault(config),
380380
redirect: to,
381381
redirectRoute: routeMap.get(destination),
382382
fallbackRoutes: [],
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'astro/config';
2+
3+
// https://astro.build/config
4+
export default defineConfig({
5+
redirects: {
6+
'/configured-redirect': '/',
7+
},
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@test/static-redirect",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineMiddleware } from 'astro:middleware';
2+
3+
export const onRequest = defineMiddleware(({ isPrerendered }, next) => {
4+
if (!isPrerendered) {
5+
throw new Error('This middleware should only run in prerendered mode.');
6+
}
7+
return next();
8+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<p>Hello Astro!</p>
6+
</body>
7+
</html>

packages/astro/test/static-build.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,16 @@ describe('Static build SSR', () => {
205205
assert.ok(await fixture.readFile('/client/.well-known/apple-app-site-association'));
206206
});
207207
});
208+
209+
describe('Static build with configured redirects', () => {
210+
it('Sets isPrerendered true in middleware', async () => {
211+
const fixture = await loadFixture({
212+
root: './fixtures/static-redirect/',
213+
});
214+
215+
await assert.doesNotReject(
216+
fixture.build(),
217+
'isPrerendered unexpectedly true during static build',
218+
);
219+
});
220+
});

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)