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

⨯ TypeError: Response body object should not be disturbed or locked #11390

Closed
breftejk opened this issue Jul 15, 2024 · 4 comments
Closed

⨯ TypeError: Response body object should not be disturbed or locked #11390

breftejk opened this issue Jul 15, 2024 · 4 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@breftejk
Copy link

Environment

  System:
    OS: macOS 15.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 178.53 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.2.0 - /opt/homebrew/bin/node
    Yarn: 4.3.1 - /opt/homebrew/bin/yarn
    npm: 10.7.0 - /opt/homebrew/bin/npm
    Watchman: 2024.05.06.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 18.0

Reproduction URL

https://github.com/Discord-Dashboard/Base-Theme

Describe the issue

Running next on external server, you cannot use next-auth. It will work, but the callback causes it to... break.

const fastify = dashboard.fastify;

const app = Next({
     dev: true,
     port: dashboard.config.port,
     dir: join(__dirname, '../'),
});
const handle = app.getRequestHandler();
await app.prepare();

fastify.all('*', (request, reply) => {
     return handle(request.raw, reply.raw);
});

Error:

  TypeError: Response body object should not be disturbed or locked
    at extractBody (node:internal/deps/undici/undici:4151:17)
    at new Request (node:internal/deps/undici/undici:5084:48)
    at new NextRequest (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/web/spec-extension/request.js:33:14)
    at NextRequestAdapter.fromNodeNextRequest (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/web/spec-extension/adapters/next-request.js:94:16)
    at NextRequestAdapter.fromBaseNextRequest (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/web/spec-extension/adapters/next-request.js:70:35)
    at doRender (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/base-server.js:1376:73)
    at cacheEntry.responseCache.get.routeKind (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/base-server.js:1587:46)
    at ResponseCache.get (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/response-cache/index.js:49:26)
    at DevServer.renderToResponseWithComponentsImpl (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/base-server.js:1507:53)
 POST /api/auth/callback/credentials 500 in 5ms

BTW: Both next.js and fastify are working just fine, the problem is with the way next-auth proceeds with the POST callback request.

How to reproduce

  • Use next.js with external server,
  • Set up next-auth,
  • Try to log in,
  • Submit the form,
  • Callback won't work.

Expected behavior

It should proceed with the callback.

@breftejk breftejk added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jul 15, 2024
@dangluan343
Copy link

@breftejk
I have the same issue.
Does anyone have the solution?

@iMidnights
Copy link

Same here! Anyone able to solve it?

@Gjum
Copy link

Gjum commented Jul 27, 2024

I solved this by moving the Next request handler into onRequest (before Fastify parses the body):

	fastify.all('*', {
		// nextjs does its own POST body parsing, must run before fastify
		onRequest: async (req, res, next) => {
			res.hijack()
			try {
				await preparedP
				const parsedUrl = URL.parse(req.url, true)
				await handleNextRequest(req.raw, res.raw, parsedUrl)
			} catch (err) {
				console.error('Error in', req.url, err)
				res.statusCode = 500
				res.raw.end('Internal server error')
			}
			next()
		},
		handler: () => {},
	})

This can probably cleaned up further, but it works.

@balazsorban44
Copy link
Member

Closing per the above comment. Sounds like an upstream issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

5 participants