vercel / next.js

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

Rewrites not working with django backend - infinite rewrites #68188

Open ShaulBenDavid opened 1 month ago

ShaulBenDavid commented 1 month ago

Link to the code that reproduces this issue

https://github.com/ShaulBenDavid/my-tubes-frontend

To Reproduce

  1. start application
  2. config rewrites

    rewrites: async () => [ { source: "/api/:path*",

    destination: ${process.env.NEXT_PUBLIC_MY_TUBES_URL}:path*, }, ],

    get infinite loop Screenshot 2024-07-26 at 12 30 28

Current vs. Expected behavior

successful api request

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.1.2
  eslint-config-next: 14.1.2
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.0.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Runtime

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

I have the same config when my backend is Express js and everything works perfect the problem is when the backend is Django rest

syuu-syuu commented 2 weeks ago

Got the same issue. Just fixed it by appending a slash to the destination url.

syuu-syuu commented 2 weeks ago

Hi I recently faced the same problem and wrote a blog post about it, which includes some solutions that worked for me. I hope it helps you as well! You can check it out here