Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(next16): resolve 'Response body object should not be disturbed or locked' error (#10226)

Arvin Xucaa9c78623de
diff --git a/src/app/(backend)/middleware/auth/index.ts b/src/app/(backend)/middleware/auth/index.ts
index ba52932ca15..6edc3d88ab4 100644
--- a/src/app/(backend)/middleware/auth/index.ts
+++ b/src/app/(backend)/middleware/auth/index.ts
@@ -33,10 +33,15 @@ export type RequestHandler = (
export const checkAuth =
(handler: RequestHandler) => async (req: Request, options: RequestOptions) => {
+ // Clone the request to avoid "Response body object should not be disturbed or locked" error
+ // in Next.js 16 when the body stream has been consumed by Next.js internal mechanisms
+ // This ensures the handler can safely read the request body
+ const clonedReq = req.clone();
+
// we have a special header to debug the api endpoint in development mode
const isDebugApi = req.headers.get('lobe-auth-dev-backend-api') === '1';
if (process.env.NODE_ENV === 'development' && isDebugApi) {
- return handler(req, { ...options, jwtPayload: { userId: 'DEV_USER' } });
+ return handler(clonedReq, { ...options, jwtPayload: { userId: 'DEV_USER' } });
}
let jwtPayload: ClientSecretPayload;
@@ -107,5 +112,5 @@ export const checkAuth =
return createErrorResponse(errorType, { error, ...res, provider: params?.provider });
}
- return handler(req, { ...options, jwtPayload });
+ return handler(clonedReq, { ...options, jwtPayload });
};
diff --git a/src/app/(backend)/trpc/async/[trpc]/route.ts b/src/app/(backend)/trpc/async/[trpc]/route.ts
index 9832373228e..f14fab4bcab 100644
--- a/src/app/(backend)/trpc/async/[trpc]/route.ts
+++ b/src/app/(backend)/trpc/async/[trpc]/route.ts
@@ -3,10 +3,15 @@ import type { NextRequest } from 'next/server';
import { pino } from '@/libs/logger';
import { createAsyncRouteContext } from '@/libs/trpc/async/context';
+import { prepareRequestForTRPC } from '@/libs/trpc/utils/request-adapter';
import { asyncRouter } from '@/server/routers/async';
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ // Clone the request to avoid "Response body object should not be disturbed or locked" error
+ // in Next.js 16 when the body stream has been consumed by Next.js internal mechanisms
+ const preparedReq = prepareRequestForTRPC(req);
+
+ return fetchRequestHandler({
// 避免请求之间互相影响
// https://github.com/lobehub/lobe-chat/discussions/7442#discussioncomment-13658563
allowBatching: false,
@@ -23,8 +28,9 @@ const handler = (req: NextRequest) =>
console.error(error);
},
- req,
+ req: preparedReq,
router: asyncRouter,
});
+};
export { handler as GET, handler as POST };
diff --git a/src/app/(backend)/trpc/desktop/[trpc]/route.ts b/src/app/(backend)/trpc/desktop/[trpc]/route.ts
index 987ca4b8650..4cb58613192 100644
--- a/src/app/(backend)/trpc/desktop/[trpc]/route.ts
+++ b/src/app/(backend)/trpc/desktop/[trpc]/route.ts
@@ -3,10 +3,15 @@ import type { NextRequest } from 'next/server';
import { pino } from '@/libs/logger';
import { createLambdaContext } from '@/libs/trpc/lambda/context';
+import { prepareRequestForTRPC } from '@/libs/trpc/utils/request-adapter';
import { desktopRouter } from '@/server/routers/desktop';
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ // Clone the request to avoid "Response body object should not be disturbed or locked" error
+ // in Next.js 16 when the body stream has been consumed by Next.js internal mechanisms
+ const preparedReq = prepareRequestForTRPC(req);
+
+ return fetchRequestHandler({
/**
* @link https://trpc.io/docs/v11/context
*/
@@ -19,7 +24,7 @@ const handler = (req: NextRequest) =>
console.error(error);
},
- req,
+ req: preparedReq,
responseMeta({ ctx }) {
const headers = ctx?.resHeaders;
@@ -27,5 +32,6 @@ const handler = (req: NextRequest) =>
},
router: desktopRouter,
});
+};
export { handler as GET, handler as POST };
diff --git a/src/app/(backend)/trpc/lambda/[trpc]/route.ts b/src/app/(backend)/trpc/lambda/[trpc]/route.ts
index 10e814e155a..550c158c3de 100644
--- a/src/app/(backend)/trpc/lambda/[trpc]/route.ts
+++ b/src/app/(backend)/trpc/lambda/[trpc]/route.ts
@@ -3,10 +3,15 @@ import type { NextRequest } from 'next/server';
import { pino } from '@/libs/logger';
import { createLambdaContext } from '@/libs/trpc/lambda/context';
+import { prepareRequestForTRPC } from '@/libs/trpc/utils/request-adapter';
import { lambdaRouter } from '@/server/routers/lambda';
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ // Clone the request to avoid "Response body object should not be disturbed or locked" error
+ // in Next.js 16 when the body stream has been consumed by Next.js internal mechanisms
+ const preparedReq = prepareRequestForTRPC(req);
+
+ return fetchRequestHandler({
/**
* @link https://trpc.io/docs/v11/context
*/
@@ -19,7 +24,7 @@ const handler = (req: NextRequest) =>
console.error(error);
},
- req,
+ req: preparedReq,
responseMeta({ ctx }) {
const headers = ctx?.resHeaders;
@@ -27,5 +32,6 @@ const handler = (req: NextRequest) =>
},
router: lambdaRouter,
});
+};
export { handler as GET, handler as POST };
diff --git a/src/app/(backend)/trpc/mobile/[trpc]/route.ts b/src/app/(backend)/trpc/mobile/[trpc]/route.ts
index 3cc78a95cdd..d6f03d68730 100644
--- a/src/app/(backend)/trpc/mobile/[trpc]/route.ts
+++ b/src/app/(backend)/trpc/mobile/[trpc]/route.ts
@@ -3,10 +3,15 @@ import type { NextRequest } from 'next/server';
import { pino } from '@/libs/logger';
import { createLambdaContext } from '@/libs/trpc/lambda/context';
+import { prepareRequestForTRPC } from '@/libs/trpc/utils/request-adapter';
import { mobileRouter } from '@/server/routers/mobile';
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ // Clone the request to avoid "Response body object should not be disturbed or locked" error
+ // in Next.js 16 when the body stream has been consumed by Next.js internal mechanisms
+ const preparedReq = prepareRequestForTRPC(req);
+
+ return fetchRequestHandler({
/**
* @link https://trpc.io/docs/v11/context
*/
@@ -19,7 +24,7 @@ const handler = (req: NextRequest) =>
console.error(error);
},
- req,
+ req: preparedReq,
responseMeta({ ctx }) {
const headers = ctx?.resHeaders;
@@ -27,5 +32,6 @@ const handler = (req: NextRequest) =>
},
router: mobileRouter,
});
+};
export { handler as GET, handler as POST };
diff --git a/src/app/(backend)/trpc/tools/[trpc]/route.ts b/src/app/(backend)/trpc/tools/[trpc]/route.ts
index 9b680d269e6..b0ff0de8570 100644
--- a/src/app/(backend)/trpc/tools/[trpc]/route.ts
+++ b/src/app/(backend)/trpc/tools/[trpc]/route.ts
@@ -3,10 +3,15 @@ import type { NextRequest } from 'next/server';
import { pino } from '@/libs/logger';
import { createLambdaContext } from '@/libs/trpc/lambda/context';
+import { prepareRequestForTRPC } from '@/libs/trpc/utils/request-adapter';
import { toolsRouter } from '@/server/routers/tools';
-const handler = (req: NextRequest) =>
- fetchRequestHandler({
+const handler = (req: NextRequest) => {
+ // Clone the request to avoid "Response body object should not be disturbed or locked" error
+ // in Next.js 16 when the body stream has been consumed by Next.js internal mechanisms
+ const preparedReq = prepareRequestForTRPC(req);
+
+ return fetchRequestHandler({
/**
* @link https://trpc.io/docs/v11/context
*/
@@ -19,8 +24,9 @@ const handler = (req: NextRequest) =>
console.error(error);
},
- req,
+ req: preparedReq,
router: toolsRouter,
});
+};
export { handler as GET, handler as POST };
diff --git a/src/libs/trpc/utils/request-adapter.ts b/src/libs/trpc/utils/request-adapter.ts
new file mode 100644
index 00000000000..f44fd4a1c98
--- /dev/null
+++ b/src/libs/trpc/utils/request-adapter.ts
@@ -0,0 +1,20 @@
+import { NextRequest } from 'next/server';
+
+/**
+ * Prepare Request object for tRPC fetchRequestHandler
+ *
+ * This function solves the "Response body object should not be disturbed or locked" error
+ * that occurs in Next.js 16 when the request body stream has been consumed or locked
+ * by Next.js internal mechanisms.
+ *
+ * By cloning the Request object, we create an independent body stream that tRPC can safely read.
+ *
+ * @see https://github.com/vercel/next.js/issues/83453
+ * @param req - The original NextRequest object
+ * @returns A cloned Request object with an independent body stream
+ */
+export function prepareRequestForTRPC(req: NextRequest): Request {
+ // Clone the Request to create an independent body stream
+ // This ensures tRPC can read the body even if the original request's body was disturbed
+ return req.clone();
+}