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

POST request from middleware fails #39060

Closed
1 task done
dferber90 opened this issue Jul 27, 2022 · 5 comments
Closed
1 task done

POST request from middleware fails #39060

dferber90 opened this issue Jul 27, 2022 · 5 comments
Labels
bug Issue was opened via the bug report template.

Comments

@dferber90
Copy link
Contributor

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
Binaries:
  Node: 17.4.0
  npm: 8.3.1
  Yarn: 1.22.19
  pnpm: 7.4.0
Relevant packages:
  next: 12.2.4-canary.2
  eslint-config-next: 12.2.3
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Fetch requests made from middleware fail but only if the request is a POST request with a request body.

Expected Behavior

The middleware should be able to make POST requests.

Link to reproduction

https://github.com/dferber90/nextjs-fetch-bug-reproduction

To Reproduce

  1. Create a new Next.js project, eg pnpm create next-app --ts.

  2. Add a middleware.ts file at the root with:

// middleware.ts
export const config = { matcher: "/" };

export async function middleware() {
  const text = await fetch("https://example.com", {
    method: "POST",
    body: "",
  }).catch((error) => {
    console.error(error);
    throw error;
  });

  console.log(text);
}
  1. Visit http://localhost:3000

You will now see the red warning box.

image

Additionally, the middleware will have logged the error to the terminal:

// from: console.error(error)
{ [TypeError: fetch failed]
  cause: [TypeError: define_process_default.nextTick is not a function] }

// from: throw error
error - Error [TypeError]: fetch failed
    at Object.processResponse (evalmachine.<anonymous>:3033:34)
    at <unknown> (evalmachine.<anonymous>:3366:42)
    at <unknown> (node:internal/process/task_queues:141:7)
    at AsyncResource.runInAsyncScope (node:async_hooks:201:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:138:8)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  middleware: true
}
@dferber90 dferber90 added the bug Issue was opened via the bug report template. label Jul 27, 2022
@dferber90
Copy link
Contributor Author

I debugged this a bit myself, maybe this is useful:

The console.error(error) output shows:

{ [TypeError: fetch failed]
  cause: [TypeError: define_process_default.nextTick is not a function] }

So define_process_default seems to be defined in

var env, versions, define_process_default;

Note that the provided default has no nextTick function:

define_process_default = { env, versions };

But nextTick is called 4 times in that file

define_process_default.nextTick(emitDrain, client);

define_process_default.nextTick(resume, this);

define_process_default.nextTick(() => {

define_process_default.nextTick((stream3, err2) => {


I also noticed that this problem only happens during local development. The problem does not seem to exist when the application is actually deployed.

@dferber90
Copy link
Contributor Author

@ghost
Copy link

ghost commented Aug 2, 2022

is there a solution yet?

@dferber90
Copy link
Contributor Author

dferber90 commented Aug 2, 2022

See #38963 (comment)

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

1 participant