vercel / next.js

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

Since 12.1.0, POST methods are not allowed (but nothing in changelog) #35185

Open adrienharnay opened 2 years ago

adrienharnay commented 2 years ago

Verify canary release

Provide environment information

Error: ENOENT: no such file or directory, realpath '/Users/mypath/frontend/info'

What browser are you using? (if relevant)

98.0.87

How are you deploying your application? (if relevant)

/

Describe the Bug

I am using Next.js as a proxy for a legacy Symfony server. Since 12.1.0, I have 405 POST method not allowed when trying to proxy POST routes, but in 12.0.7 it works. I found nothing mentioning this change in the changelog.

Expected Behavior

No changes between 12.0.7 and 12.1.0

To Reproduce

In next.config.js:

rewrites: () => {
        return {
          fallback: [
            {
              source: '/:path*',
              destination: `${process.env.SYMFONY_URL}/:path*`,
            },
          ],
        };
      },

Where SYMFONY_URL points to a server with POST routes that are not implemented by the Next.js server.

Try hitting a POST route.

alisalahio commented 2 years ago

+1 We're facing the same issue in our intercom app embed (because Intercom sends a post request to the page)

leerob commented 2 years ago

Where are you hosting Next.js? (I know @alisalahio is on Vercel 😄)

adrienharnay commented 2 years ago

Hey! This is hosted on Kubernetes.

alisalahio commented 2 years ago

@leerob Vercel FTW 🔥

@thepinger solved the intercom embed issue temporarily by creating an api route that accepts the post request and redirects to the embedded page (instead of embedding the status page directly)

export default async function handler(req, res) {
  if (req.method === 'POST') {
    res.redirect(302, '/')
  }
}
abhishekdev commented 2 years ago

This seems related to NextJs changes that added explicit rules to disable POST for static pages ref: https://github.com/vercel/next.js/blob/7d52589bd610c9770f3acafb8384e166f91c4400/packages/next/server/next-server.ts#L851-L865

A similar use case was previously handled in https://github.com/vercel/next.js/pull/8323 and a related test case

Potential fix using middleware

import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

// TODO: Remove this based on resolution for https://github.com/vercel/next.js/issues/35185
export function middleware(req: NextRequest) {
    // Only execute for specific pathnames if running this needs to run for at "/pages" to avoid running for all requests
    if (req.method === 'POST' && req.nextUrl.pathname === '/') {
        // Redirect to the correct static page using semantic 303
        // ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
        return NextResponse.redirect(req.nextUrl.clone(), 303);
    }
}
adrienharnay commented 2 years ago

Hi @abhishekdev, I'm not getting middlewares to work with Next 12.2.0, so I can't try your workaround...

Are there any news on this issue?

adrienharnay commented 6 months ago

Hi @balazsorban44. I upgraded to Next.js 14.2.2 but this issue still persists. I think the label is still needed.