Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rewrite): match index with params #11065

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-cycles-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a bug in the Astro rewrite logic, where rewriting the index with parameters - `next("/?foo=bar")` - didn't work as expected.
14 changes: 10 additions & 4 deletions packages/astro/src/core/app/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export class AppPipeline extends Pipeline {
return module.page();
}

async tryRewrite(payload: RewritePayload): Promise<[RouteData, ComponentInstance]> {
async tryRewrite(
payload: RewritePayload,
request: Request
): Promise<[RouteData, ComponentInstance]> {
let foundRoute;

for (const route of this.#manifestData!.routes) {
Expand All @@ -86,9 +89,12 @@ export class AppPipeline extends Pipeline {
foundRoute = route;
break;
}
} else if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
} else {
const newUrl = new URL(payload, new URL(request.url).origin);
if (route.pattern.test(decodeURI(newUrl.pathname))) {
foundRoute = route;
break;
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/core/base-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export abstract class Pipeline {
*
* @param {RewritePayload} rewritePayload
*/
abstract tryRewrite(rewritePayload: RewritePayload): Promise<[RouteData, ComponentInstance]>;
abstract tryRewrite(
rewritePayload: RewritePayload,
request: Request
): Promise<[RouteData, ComponentInstance]>;

/**
* Tells the pipeline how to retrieve a component give a `RouteData`
Expand Down
14 changes: 10 additions & 4 deletions packages/astro/src/core/build/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ export class BuildPipeline extends Pipeline {
}
}

async tryRewrite(payload: RewritePayload): Promise<[RouteData, ComponentInstance]> {
async tryRewrite(
payload: RewritePayload,
request: Request
): Promise<[RouteData, ComponentInstance]> {
let foundRoute: RouteData | undefined;
// options.manifest is the actual type that contains the information
for (const route of this.options.manifest.routes) {
Expand All @@ -291,9 +294,12 @@ export class BuildPipeline extends Pipeline {
foundRoute = route;
break;
}
} else if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
} else {
const newUrl = new URL(payload, new URL(request.url).origin);
if (route.pattern.test(decodeURI(newUrl.pathname))) {
foundRoute = route;
break;
}
}
}
if (foundRoute) {
Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
AstroGlobalPartial,
ComponentInstance,
MiddlewareHandler,
MiddlewareNext,
RewritePayload,
RouteData,
SSRResult,
Expand Down Expand Up @@ -118,7 +117,7 @@ export class RenderContext {
if (payload) {
if (this.pipeline.manifest.rewritingEnabled) {
try {
const [routeData, component] = await pipeline.tryRewrite(payload);
const [routeData, component] = await pipeline.tryRewrite(payload, this.request);
this.routeData = routeData;
componentInstance = component;
} catch (e) {
Expand Down Expand Up @@ -212,7 +211,7 @@ export class RenderContext {
const rewrite = async (reroutePayload: RewritePayload) => {
pipeline.logger.debug('router', 'Called rewriting to:', reroutePayload);
try {
const [routeData, component] = await pipeline.tryRewrite(reroutePayload);
const [routeData, component] = await pipeline.tryRewrite(reroutePayload, this.request);
this.routeData = routeData;
if (reroutePayload instanceof Request) {
this.request = reroutePayload;
Expand Down Expand Up @@ -398,7 +397,7 @@ export class RenderContext {
const rewrite = async (reroutePayload: RewritePayload) => {
try {
pipeline.logger.debug('router', 'Calling rewrite: ', reroutePayload);
const [routeData, component] = await pipeline.tryRewrite(reroutePayload);
const [routeData, component] = await pipeline.tryRewrite(reroutePayload, this.request);
this.routeData = routeData;
if (reroutePayload instanceof Request) {
this.request = reroutePayload;
Expand Down
14 changes: 10 additions & 4 deletions packages/astro/src/vite-plugin-astro-server/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ export class DevPipeline extends Pipeline {
}
}

async tryRewrite(payload: RewritePayload): Promise<[RouteData, ComponentInstance]> {
async tryRewrite(
payload: RewritePayload,
request: Request
): Promise<[RouteData, ComponentInstance]> {
let foundRoute;
if (!this.manifestData) {
throw new Error('Missing manifest data. This is an internal error, please file an issue.');
Expand All @@ -209,9 +212,12 @@ export class DevPipeline extends Pipeline {
foundRoute = route;
break;
}
} else if (route.pattern.test(decodeURI(payload))) {
foundRoute = route;
break;
} else {
const newUrl = new URL(payload, new URL(request.url).origin);
if (route.pattern.test(decodeURI(newUrl.pathname))) {
foundRoute = route;
break;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/astro/test/fixtures/reroute/src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const second = async (context, next) => {
if (context.url.pathname.includes('/auth/base')) {
return await next('/');
}

if (context.url.pathname.includes('/auth/params')) {
return next('/?foo=bar');
}
}
return next();
};
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/reroute/src/pages/auth/params.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>Index with params</title>
</head>
<body>
<h1>Index with params</h1>
</body>
</html>
7 changes: 7 additions & 0 deletions packages/astro/test/rewrite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,11 @@ describe('Middleware', () => {
assert.equal($('h1').text(), 'Index');
assert.equal($('p').text(), '');
});

it('should render the index when rewriting with params', async () => {
const html = await fixture.fetch('/auth/params').then((res) => res.text());
const $ = cheerioLoad(html);

assert.match($('h1').text(), /Index/);
});
});
Loading