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

GET and PUT route in same route handler causes 405 Method Not Allowed #66647

Open
chukwumaokere opened this issue Jun 7, 2024 · 3 comments
Open
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@chukwumaokere
Copy link

Link to the code that reproduces this issue

https://github.com/chukwumaokere/app-route-get-put-bug-minimal-repro

To Reproduce

  1. Clone the repo
  2. Start the server locally pnpm dev
  3. hit the http://localhost:3000/api/something route with a GET request. See that it works as expected. The request is handled
  4. hit the http://localhost:3000/api/something route with a PUT request. See that it works as expected. The request is handled
  5. Run pnpm run build to build a production verison
  6. Run pnpm start to run the production build
  7. hit the http://localhost:3000/api/something route with a GET request. See that it works as expected. The request is handled
  8. hit the http://localhost:3000/api/something route with a PUT request. See that it DOES NOT works as expected. The request is NOT handled. You will receive an HTTP Status 405 Method Not Allowed.

Current vs. Expected behavior

I expected when I hit the PUT endpoint on a production build, even if its next to a GET endpoint, it should work.
Instead I receive a 405 when deployed and when built for production locally. I do NOT get this error when running in dev mode pnpm dev so there is no way for me to validate unless I build a production version locally or wait until its deployed.

Here is a screenshot of this running locally on a production build and returning error 405.
image

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 32713
  Available CPU cores: 16
Binaries:
  Node: 22.1.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.2.3
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
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 build (local), next start (local), Vercel (Deployed)

Additional context

This issue has been ongoing since Next13 and I've tried researching a resolution but I guess this isn't something people have encountered often since I couldn't find any reports of this issue. It exists into Next 14 and probably will continue to exist. There is a workaround which is just moving the GET or PUT route to its OWN route folder i.e., /api/something/add for PUT.

@chukwumaokere chukwumaokere added the bug Issue was opened via the bug report template. label Jun 7, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Jun 7, 2024
@chukwumaokere
Copy link
Author

FYI I havent tested this with other routes. Like PUT and POST in the same file, only GET and PUT

@Firas88Alkhatib
Copy link

opt out of caching seems to solve this.
export const dynamic = "force-dynamic";

@chukwumaokere
Copy link
Author

Can confirm that opting out of caching solves this issue but its not a valid solution in the off chance you want to cache the "cacheable" requests like GET but not cache requests like PUT

Also if nothing else, the error should not be "Method not allowed", this could be a development runtime error with some helpful steps for devs to know this sort of this just isnt supported and they can either move the route, or opt out of caching to resolve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

2 participants