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

Regression: fetch requests in middleware fail with a body #38963

Closed
1 task done
ottomated opened this issue Jul 24, 2022 · 8 comments
Closed
1 task done

Regression: fetch requests in middleware fail with a body #38963

ottomated opened this issue Jul 24, 2022 · 8 comments
Labels
Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@ottomated
Copy link

Verify canary release

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

Provide environment information

    Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Fri, 15 Jul 2022 15:33:02 +0000
    Binaries:
      Node: 16.15.0
      npm: 8.5.5
      Yarn: 3.2.1
      pnpm: N/A
    Relevant packages:
      next: 12.2.4-canary.0
      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

Making a fetch request from middleware that includes a request body fails with TypeError: fetch failed.

Tested in 12.2.2 - succeeds. Fails in 12.2.3 and canary.

Expected Behavior

The fetch request should succeed.

Link to reproduction

https://codesandbox.io/s/jovial-fast-yflq3w?file=/middleware.ts

To Reproduce

  1. Load any page
  2. Observe the error in the console
  3. Remove the line body: ""
  4. Observe that there is no error
@ottomated ottomated added the bug Issue was opened via the bug report template. label Jul 24, 2022
@joaogarin
Copy link

Maybe take a look in https://nextjs.org/docs/messages/middleware-upgrade-guide#no-response-body seems like this has changed in 12.2 so the behaviour is intentional :

"If your Middleware does respond with a body, a runtime error will be thrown"

@balazsorban44
Copy link
Member

balazsorban44 commented Jul 25, 2022

@joaogarin that only applies to responding (ie: return) from the Middleware. Using fetch should otherwise work as expected.

I could verify in 12.2.3-canary.17 and upwards, so this seems to be related to #38862, thanks for reporting.

@ghost
Copy link

ghost commented Aug 2, 2022

is there a solution for that yet?

@dferber90
Copy link
Contributor

@Charlie-007-star Yes, see vercel/edge-runtime#68

@balazsorban44
Copy link
Member

#39219 included the fix for this and will be available in the next next@canary release.

@balazsorban44
Copy link
Member

This should be out on 12.2.4-canary.11, please check it out!

@trm217
Copy link

trm217 commented Aug 11, 2022

@balazsorban44 This error started occurring in a project using next@12.2.3.
Unfortunately it is still happening with next@12.2.4.

The code you see below is executed directly in the middleware and still causes the [TypeError] fetch failed exception.

import { NextRequest } from 'next/server'

/**
 * Function to fetch the roles of the user and updated cookies
 * @param req
 */
async function fetchMeObject(
  req: NextRequest,
): Promise<{ me: null | { roles: string[] }; cookie: string }> {
  const response = await fetch(process.env.API_URL, {
    method: 'POST',
    body: JSON.stringify({ query: 'query { me { roles } }' }),
    headers: {
      'Content-Type': 'application/json',
      // Attach headers to the request to ensure `me` is returned
      Cookie: req.headers.get('cookie'),
    },
  })

  if (response.ok) {
    const { data } = await response.json()

    return {
      me: data?.me,
      cookie: response.headers.get('set-cookie'),
    }
  }

  // Return the updated cookie-header in case the request was not successful
  return {
    me: null,
    cookie: response.headers.get('set-cookie'),
  }
}

export default fetchMeObject

@github-actions
Copy link
Contributor

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 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

5 participants