-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Unexpected middleware behavior: Cannot use Astro.rewrite after the request body has been read #13844
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
Comments
This is a bug about how we handle the carried paylod inside
I tried using only |
@ematipico I don't exactly see what the issue is with |
Is this a bug? |
I am trying to grasp how the whole middleware/rewrite flow is implemented in Astro, but it is scattered around so many files that I am having a hard time. For what it is worth: |
In case this might help for debugging this issue: I tried creating a simplified version of the export function simplifiedSequence(
...handlers: MiddlewareHandler[]
): MiddlewareHandler {
return defineMiddleware((context, next) => {
/** Used to carry the rerouting payload across middleware functions. */
let finalRewriteTo: string;
function runHandler(i: number) {
const handler: any = handlers[i];
return handler(context, async (rewriteTo?: string) => {
if (i < handlers.length - 1) {
if (rewriteTo) {
finalRewriteTo = rewriteTo;
}
return runHandler(i + 1);
} else {
return next(finalRewriteTo);
}
});
}
return runHandler(0);
});
} In the original function, there are these particular lines: astro/packages/astro/src/core/middleware/sequence.ts Lines 39 to 49 in fbcfa68
astro/packages/astro/src/core/middleware/sequence.ts Lines 77 to 80 in fbcfa68
As I understand it, the purpose of creating However, it seems that all middleware handlers run first, and only then the actual rewrite happens (triggered by the final So this raises the question: Should the context be updated for the remaining handlers in the sequence, even though the rewrite hasn’t been executed yet? This simplified version of the My current guess is that the issue is somewhere in the astro/packages/astro/src/core/render-context.ts Lines 154 to 265 in fbcfa68
Any help is welcome 🕵 |
In your example, what's the idea behind |
The actual implementation in the website is more advanced and has a logical use-case. But I ran into this issue. So I created this heavily simplified sample just to illustrate how one can run into the problem. In short you could say: the rewrite in this middleware handler allows the Another example could be:
|
Update: So I tried creating the most minimal reproduction locally and I found that the issue might actually be with Bun (which I use to run astro). Somehow Bun is already consuming the request body somewhere? // src/middleware.ts
import { sequence } from "astro/middleware";
export const onRequest = sequence((ctx, next) => {
if (ctx.url.pathname == "/oldlink") {
return next("/newlink");
}
return next();
}); // src/pages/newlink.ts
import type { APIRoute } from "astro";
export const POST: APIRoute = async ({ request }) => {
const data = await request.json();
return new Response(JSON.stringify(data), { status: 200 });
}; // package.json
///...
"scripts": {
"devnode": "astro dev"
"devbun": "bunx --bun astro dev"
}
//... When I run this setup with Node ( My minimal reproducible example (which runs on Node) still stands though. So maybe this is a broader issue? |
Update: I do still think this issue is worth investigating further. Looking at these docs: https://docs.astro.build/en/guides/middleware/#rewriting |
Uh oh!
There was an error while loading. Please reload this page.
Astro Info
Describe the Bug
In my middleware there might be some intentional rewrites. But I ran into some unexpected behavior where I got the error:
I created a minimal reproducible example on Stackblitz.
src/middleware.ts
file.I don't understand why though. The error mentions "If you need to read the body, first clone the request", but I am doing that. Why/where is
Astro.rewrite()
called anyway?Am I misunderstanding this, or could this be a bug?
Perhaps someone could enlighten me? 😇
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-8mju777p?file=src%2Fmiddleware.ts
The text was updated successfully, but these errors were encountered: