-
Notifications
You must be signed in to change notification settings - Fork 6
Description
We stumbled on an issue once we specified "logRequestDetails" in the logger middleware. Specifying this requires feeding a cloned request object to the middleware; otherwise, all kinds of issues appear once you try to access the request object at a later point. In our opinion, this (cloning the request object) should be done internally by the logger middleware to avoid any side effects further down the line. At the very least, this behaviour should be documented in your documentation.
Edit:
On further investigation .. The issue is deeper than missing cloning. On Edge runtimes (Netlify Edge/Deno, Vercel Edge), the Web Streams API only allows a body to be consumed once. When the logger middleware touches the request, it corrupts the body stream for all downstream consumers (server actions, route handlers).
request.clone() doesn't help — it tees the stream (still corrupts downstream) and loses NextRequest properties like nextUrl.hostname.
Only reliable workaround: pass a body-less request to the logger:
const logReq = new NextRequest(request.url, {
method: request.method,
headers: request.headers,
// no body — intentionally omitted
});
logger.middleware(logReq, { logRequestDetails: ["method", "url"] });
The library should never consume the request body in middleware mode, or at least construct an internal body-less copy. Currently any use of logRequestDetails in middleware/proxy on Edge runtimes silently breaks downstream request handling.