vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.39k stars 26.78k forks source link

next@13.0.2 with `app` introduced fetch redirect bug from undici@5.12.0 #44132

Closed jaslong closed 1 year ago

jaslong commented 1 year ago

Verify canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000
Binaries:
  Node: 16.16.0
  npm: 8.11.0
  Yarn: 1.22.15
  pnpm: 7.9.3
Relevant packages:
  next: 13.0.7
  eslint-config-next: 13.0.5
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

https://github.com/nodejs/undici/commit/e49b68d68d0d1c6fada5f92dc8ccb5c6d87d48f6

To Reproduce

Not a full repro, but see test in https://github.com/nodejs/undici/commit/e49b68d68d0d1c6fada5f92dc8ccb5c6d87d48f6 that shows how a redirect reproduces the bug.

Describe the Bug

In undici@5.12.0, a bug handling redirects was introduced. This bug was fixed in undici@5.14.0.

Next upgraded to undici@5.12.0 in commit https://github.com/vercel/next.js/commit/137008e6133e419ebf87111c225a45156d804511 which is affecting next@13.0.2 and above. I believe the fix is as simple as upgrading to undici@5.14.0, which I'm working on doing right now.

The bug manifests itself indeterministically--requests with redirects would fail randomly when running build or dev scripts.

For example, when running build, the stack track might look something like this:

info  - Creating an optimized production build  
info  - Compiled successfully
info  - Linting and checking validity of types  
info  - Collecting page data ..TypeError: fetch failed
    at Object.fetch (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:1:26684)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async r.fetchData (/Users/foo/nextjs13-example/.next/server/chunks/710.js:965:103)
  cause: TypeError: Cannot read properties of undefined (reading 'reason')
      at makeAppropriateNetworkError (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:54604)
      at schemeFetch (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:30000)
      at /Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:28607
      at mainFetch (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:29007)
      at httpRedirectFetch (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:33708)
      at httpFetch (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:32580)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async schemeFetch (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:30793)
      at async /Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:28601
      at async mainFetch (/Users/foo/nextjs13-example/node_modules/next/dist/compiled/undici/index.js:2:28426)

Expected Behavior

Fetches with redirects should work

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

github-actions[bot] commented 1 year ago

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.