Search code examples
next.js

Is there a way to disable all next.js api routes in production environment?


During the developing phase, API routes are good for me with Mock data.

While in the Production environment, I have my own backend service.

Is there a way to disable all the API routes?


Solution

  • Thanks for @juliomalves' suggestion, I found it can add redirect rules in next.config.js to disable (redirect to 404) all the /api/* requests 💚💙🤎

    const nextConfig = {
      redirects: () => {
        if (process.env.NODE_ENV === "production") {
          return [
            {
              source: "/api/:slug*",
              destination: '/404',
              permanent: true,
            }
          ];
        } else {
          return []
        }
      },
      ...
    }