Nader's Daily Blog
Welcome to Every Developers favorite blog in the Devosphere

New product features | The latest in technology | The weekly debugging nightmares & more!

How to use server actions with static pages in Next js 13.4

How to use server actions with static pages in Next js 13.4

July 17, 2023

Nader Elmahdy

Nader Elmahdy

Take my knowledge and shove it up your brain...

Fix the 405 error that pops up when using server actions in a page that uses generateStaticParams()

Next js

In Next js 13, If you try to use a server action on a page that uses generateStaticParams() like for example, if you have an e-commerce store, you want the product page to be generated at build time, and you want the users to be able to add the product to the cart using a server action.

If you try to do that, the server action won't work, and if you open the console, you'll see this error:

The reason for that is Post requests aren't supported in SSG pages.

So how do we fix this...

We don't.

I found that this error occurs in development only.

If you run your project in production (npm run build then npm start), You won't get this error and the server action will work as expected.

If you really wanna fix this in development, you can check this pull, apparently enabling preview mode should fix it, but I don't really care, It works fine in production and that's what matters.

You might also like: