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

[NEXT-1167] Server Actions 405 "Method Not Allowed" when using generateStaticParams #49408

Closed
1 task done
arduano opened this issue May 7, 2023 · 30 comments · Fixed by #51534
Closed
1 task done

[NEXT-1167] Server Actions 405 "Method Not Allowed" when using generateStaticParams #49408

arduano opened this issue May 7, 2023 · 30 comments · Fixed by #51534
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@arduano
Copy link

arduano commented May 7, 2023

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 Wed, 26 Apr 2023 20:50:14 +0000
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.4.1
      eslint-config-next: 13.0.6-canary.1
      react: 18.2.0
      react-dom: 18.2.0

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

No response
Server actions. It seems like server actions wasn't part of the issue form dropdown earlier

Link to the code that reproduces this issue

https://github.com/arduano/server-actions-repro

To Reproduce

  • npm install
  • npm run dev
  • Open /asdf in the browser
  • Click the button, observe the network requests in the browser's dev tools

Describe the Bug

Including generateStaticParams in a page breaks server actions, making it return 405 on each request

Expected Behavior

The server actions behave as expected

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1167

@arduano arduano added the bug Issue was opened via the bug report template. label May 7, 2023
@arduano arduano changed the title Server Actions 405 "Method Not Allowed" Server Actions 405 "Method Not Allowed" when using generateStaticParams May 7, 2023
@AlexisWalravens
Copy link

Hi, can confirm just had the same bug.
The moment I remove generateStaticParams from the page the action works.

@timneutkens timneutkens added the linear: next Confirmed issue that is tracked by the Next.js team. label May 17, 2023
@timneutkens timneutkens changed the title Server Actions 405 "Method Not Allowed" when using generateStaticParams [NEXT-1167] Server Actions 405 "Method Not Allowed" when using generateStaticParams May 17, 2023
@TNury
Copy link

TNury commented May 17, 2023

Same thing here, the moment I removed generateStaticParams, it worked

@trinhthinh388

This comment was marked as spam.

@NaderTate
Copy link

any updates on this issue?

@emondpph
Copy link

Also had the same issue with 405 http errors and errors in the console... Commenting the generateStaticParams function fixes. When are you expecting to publish the fix ?

@simpel
Copy link

simpel commented Jun 29, 2023

Same for me! Trying to use server actions gave me 405 all the time. Removing generateStaticParams made the server actions work...but...yea.. I need generateStaticParams too! :)

@Tommoore96
Copy link

I'm not using generateStaticParams but I am getting the same error (405) when trying to use server actions.

My server action takes files as form data and uploads those files to google cloud.

@arduano
Copy link
Author

arduano commented Jul 3, 2023

@Tommoore96 Would you be able to get a minimal reproduction? Your situation might need to be a separate github issue.

@AslanArt
Copy link

AslanArt commented Jul 3, 2023

Same here I'm not using generateStaticParams but still get 405 errors, I am not able to use my api files somehow! Work perfectly in local

@hack89
Copy link

hack89 commented Jul 7, 2023

Same here. When I removed generateStaticParams, it worked.

@walid-mos
Copy link

walid-mos commented Jul 8, 2023

Trying to do internationalization, and in my end the same problem.

I am using generateStaticParams in the root layout, then server actions in all my routes get 405 method not allowed.

Removing generateStaticParams make it works, but it is not a fix as i need this for locales.

This is related to : #49373 (comment)

@tarunsharma18
Copy link

Facing the same issue when using server actions along with getStaticParams.
When can I expect the fix for the same?

@amlcodes
Copy link

Bump

Is there anybody from Next on this issue? Happy to look into into it.

@ctrenks
Copy link

ctrenks commented Jul 29, 2023

Do we know if this is a bug or can we not use server actions with generateStaticParams pages?

@NaderTate
Copy link

This error occurs in development only. Try building the app then running it in production, it will work just fine.

@ctrenks
Copy link

ctrenks commented Jul 31, 2023

405 error in production on [POST] from Vercel logs. Page renders static content correctly then errors on Server action POST...

@plevik
Copy link

plevik commented Aug 3, 2023

Behaviour is still present on "next": "13.4.13-canary.10".

@Arctomachine
Copy link

This error occurs in development only. Try building the app then running it in production, it will work just fine.

Error happens in production too, just not printing anything in server console (browser console and network tab still show 405)

@kfirfitousi
Copy link

Did anyone manage to find a workaround for this?

@AndrewRPorter
Copy link

I was adding i18n support to my website following this guide and ran into this issue as well. In my case, I was getting failures with server actions when my layout file was in my app/[lang] directory.

Splitting out the layout files into two, one at the app route (app/layout.tsx) and one in the app/[lang] dir (app/[lang]/layout.tsx) folder did it for me.

@roeean
Copy link

roeean commented Sep 2, 2023

Also, I have the same issue. I am removing the generateStaticParams function, and the server action works property. Why can't we use both generateStaticParams and server actions?

@Bartek532

This comment has been minimized.

@cmoscosoz
Copy link

Same issue here. with generateStaticParams

I was adding i18n support to my website following this guide and ran into this issue as well. In my case, I was getting failures with server actions when my layout file was in my app/[lang] directory.

Splitting out the layout files into two, one at the app route (app/layout.tsx) and one in the app/[lang] dir (app/[lang]/layout.tsx) folder did it for me.

@AndrewRPorter were you using generateStaticParams? I'm using i18n too. could you please provide an example of how you split your layouts?

@mohamedzhioua
Copy link

I was adding i18n support to my website following this guide and ran into this issue as well. In my case, I was getting failures with server actions when my layout file was in my app/[lang] directory.

Splitting out the layout files into two, one at the app route (app/layout.tsx) and one in the app/[lang] dir (app/[lang]/layout.tsx) folder did it for me.

any other way to solve it ??? i have the same issue after adding app/[lang]

@mohamedzhioua
Copy link

generateStaticParams

same here i just tried it and it works for me too

@qnten
Copy link

qnten commented Sep 11, 2023

Didn't find this issue at first because it doesn't include the error message I am getting. In case anyone else gets the same error message, here it is:

Warning: Detected multiple renderers concurrently rendering the same context provider. This is currently unsupported.
    at PathnameContextProviderAdapter (/reproduction-app/node_modules/.pnpm/next@13.4.20-canary.23_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/shared/lib/router/adapters.shared-runtime.js:84:11)
    at Be (/reproduction-app/node_modules/.pnpm/next@13.4.20-canary.23_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/pages.runtime.dev.js:1:47861)
    at Ze (/reproduction-app/node_modules/.pnpm/next@13.4.20-canary.23_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/pages.runtime.dev.js:1:48550)
    at div
    at Ye (/reproduction-app/node_modules/.pnpm/next@13.4.20-canary.23_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/pages.runtime.dev.js:1:55624)

@irg1008
Copy link

irg1008 commented Sep 12, 2023

We are talking about it in the next-intl repo as well: amannn/next-intl#353

I think next.js should look into this if they plan to release server actions as beta or stable soon

@kodiakhq kodiakhq bot closed this as completed in #51534 Sep 14, 2023
kodiakhq bot pushed a commit that referenced this issue Sep 14, 2023
### What?
Pages marked with `generateStaticParams` don't currently support server actions, and instead return a 405 Method Not Allowed, with no action being taken on the client. Additionally, pages that are marked static & use server actions are opted into dynamic rendering.

### Why?
The page that has `generateStaticParams` is marked as `isSSG` [here](https://github.com/ztanner/next.js/blob/ee2ec3dd1de2f5cdd26ddc64c1036f02c92e1888/packages/next/src/server/base-server.ts#L1337).

As a result, the request is short-circuited because a POST request isn't supported on static pages. Upon detecting a server action on a page marked SSG, we bypass the static cache and go straight to the lambda. 

This PR introduces an experimental option to the prerender manifest that will allow for selectively bypassing the static cache

This also removes the need to bail out of static generation

Closes NEXT-1167
Closes NEXT-1453
Fixes #49408
Fixes #52840
Fixes #50932
@irg1008
Copy link

irg1008 commented Sep 14, 2023

OMG 😱 Thank you! Amazing work

@borispoehland
Copy link

borispoehland commented Sep 15, 2023

I found a workaround

You can currently bypass the error by turning your page from mypath/page.tsx to mypath/[[...slug]].tsx. This optional catch-all segment makes "/mypath" still accessible and magically works, by telling Next.js: This page must be dynamic. Inside of the route's page.tsx, you can then make sure that only "/mypath" is matched by using notFound:

// mypath/[[...slug]].tsx
export default function Page({ params: { slug } }) {
  if (slug?.[0]) { notFound() }
  
  return <form action={myServerAction}> <button type="submit">Works like a charm</button> </form>

}

Made a YT video as well: https://youtu.be/K6Jn24FsZFc

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. 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 30, 2023
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. linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.