Skip to content

Commit 976879a

Browse files
authored
fix(routing): update routePattern when using sequence (#14132)
1 parent 3572d85 commit 976879a

File tree

11 files changed

+82
-1
lines changed

11 files changed

+82
-1
lines changed

.changeset/dull-poems-obey.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 a bug where the property `Astro.routePattern`/`context.routePattern` wasn't updated when using a rewrite via middleware.

packages/astro/src/core/middleware/sequence.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler {
8080
handleContext.url = new URL(newRequest.url);
8181
handleContext.cookies = new AstroCookies(newRequest);
8282
handleContext.params = getParams(routeData, pathname);
83+
handleContext.routePattern = routeData.route;
8384
setOriginPathname(
8485
handleContext.request,
8586
oldPathname,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { defineConfig } from 'astro/config';
2+
3+
// https://astro.build/config
4+
export default defineConfig({});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@test/rewrite-route-pattern",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*"
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineMiddleware, sequence } from 'astro:middleware';
2+
3+
export const first = defineMiddleware(async (context, next)=> {
4+
if (context.url.pathname === '/index2') {
5+
return next('/123/post')
6+
} else {
7+
8+
return next(`/destination`);
9+
}
10+
});
11+
12+
export const second = defineMiddleware(async (context, next)=> {
13+
context.locals.pattern = context.routePattern;
14+
return next();
15+
});
16+
17+
export const onRequest = sequence(first, second);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
const pattern = Astro.locals.pattern;
3+
---
4+
5+
<p>{pattern}</p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
const pattern = Astro.locals.pattern;
3+
---
4+
5+
<p>{pattern}</p>

packages/astro/test/fixtures/rewrite-route-pattern/src/pages/index.astro

Whitespace-only changes.

packages/astro/test/fixtures/rewrite-route-pattern/src/pages/index2.astro

Whitespace-only changes.

packages/astro/test/rewrite.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ describe('Runtime error in SSR, custom 500', () => {
730730
});
731731
});
732732

733-
describe('Issue 13633', async () => {
733+
describe('Rewrite issue 13633', async () => {
734734
/** @type {import('./test-utils').Fixture} */
735735
let fixture;
736736
let devServer;
@@ -754,3 +754,33 @@ describe('Issue 13633', async () => {
754754
assert.equal($('h1').text(), 'Index page');
755755
});
756756
});
757+
758+
describe('Rewrite', async () => {
759+
/** @type {import('./test-utils').Fixture} */
760+
let fixture;
761+
let devServer;
762+
before(async () => {
763+
fixture = await loadFixture({
764+
root: './fixtures/rewrite-route-pattern/',
765+
output: 'server',
766+
adapter: testAdapter(),
767+
});
768+
devServer = await fixture.startDevServer();
769+
});
770+
771+
after(async () => {
772+
await devServer.stop();
773+
});
774+
775+
it('should correctly update routePattern with using sequence in middleware', async () => {
776+
let html = await fixture.fetch('/').then((res) => res.text());
777+
let $ = cheerioLoad(html);
778+
779+
assert.equal($('p').text(), '/destination');
780+
781+
html = await fixture.fetch('/index2').then((res) => res.text());
782+
$ = cheerioLoad(html);
783+
784+
assert.equal($('p').text(), '/[id]/post');
785+
});
786+
});

0 commit comments

Comments
 (0)