Skip to content

Commit 34e6b3a

Browse files
ematipicoascorbic
andauthored
fix(routing): static redirect with prerendered route (#14170)
Co-authored-by: ascorbic <[email protected]>
1 parent c86b5bb commit 34e6b3a

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

.changeset/rude-sites-heal.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 issue where static redirects couldn't correctly generate a redirect when the destination is a prerendered route, and the `output` is set to `"server"`.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,20 @@ export async function createRoutesList(
509509
for (const route of routes) {
510510
promises.push(
511511
limit(async () => {
512-
if (route.type !== 'page' && route.type !== 'endpoint') return;
512+
if (route.type !== 'page' && route.type !== 'endpoint' && route.type !== 'redirect') return;
513+
// External redirects aren't taken into account
514+
if (route.type === 'redirect' && !route.redirectRoute) return;
513515
const localFs = params.fsMod ?? nodeFs;
514516
const content = await localFs.promises.readFile(
515-
fileURLToPath(new URL(route.component, settings.config.root)),
517+
fileURLToPath(
518+
new URL(
519+
// The destination redirect might be a prerendered
520+
route.type === 'redirect' && route.redirectRoute
521+
? route.redirectRoute.component
522+
: route.component,
523+
settings.config.root,
524+
),
525+
),
516526
'utf-8',
517527
);
518528

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export async function getRoutePrerenderOption(
1616
const match = PRERENDER_REGEX.exec(content);
1717
if (match) {
1818
route.prerender = match[1] === 'true';
19+
if (route.redirectRoute) {
20+
route.redirectRoute.prerender = match[1] === 'true';
21+
}
1922
}
2023

2124
await runHookRouteSetup({ route, settings, logger });
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
export const prerender = true;
3+
4+
export function getStaticPaths() {
5+
return [{ params: { dynamic:'hello', prerender:'world' } }]
6+
}
7+
---
8+
{JSON.stringify(Astro.params)}
9+
10+
<a href="/">home</a>

packages/astro/test/redirects.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ describe('Astro.redirect', () => {
2121
'/source/[dynamic]': '/not-verbatim/target1/[dynamic]',
2222
// may be called src/pages/not-verbatim/target2/[abc]/[xyz].astro
2323
'/source/[dynamic]/[route]': '/not-verbatim/target2/[dynamic]/[route]',
24+
// check for prerendered routes
25+
'/source/[dynamic]/[prerender]': '/not-verbatim/target2/[dynamic]/[prerender]',
2426
// may be called src/pages/not-verbatim/target3/[...rest].astro
2527
'/source/[...spread]': '/not-verbatim/target3/[...spread]',
2628
},

0 commit comments

Comments
 (0)