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

Backport request: Server action that fails in middleware fails silently #73043

Open
nphmuller opened this issue Nov 21, 2024 · 1 comment
Open
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@nphmuller
Copy link
Contributor

nphmuller commented Nov 21, 2024

Link to the code that reproduces this issue

https://github.com/nphmuller/next-server-action-middleware-error

To Reproduce

  1. npm run dev
  2. Click button

Code from repro:

middleware.ts:

export default async function Middleware(request: NextRequest) {
  if (request.method === "POST") {
    return NextResponse.json("Example: token invalid", { status: 401 });
  }
}

page.tsx:

export default function Home() {
  return (
    <button
      onClick={async () => {
        try {
          await actionThatThrowsViaMiddleware();
        } catch {
          console.log("Caught error");
        }
      }}
    >
      Click for action that throws
    </button>
  );
}

actions.ts:

"use server";

export const actionThatThrowsViaMiddleware = async () => {};

Current vs. Expected behavior

Expected: Console logs Caught error

Actual Next 14: No console log
Actual Next 15: Console logs Caught error

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.13.0
  npm: 10.9.0
  Yarn: N/A
  pnpm: 9.6.0
Relevant Packages:
  next: 14.2.18 // An outdated version detected (latest is 15.0.3), upgrade is highly recommended!
  eslint-config-next: 15.0.3
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.6.3
Next.js Config:
  output: N/A
 ⚠ An outdated version detected (latest is 15.0.3), upgrade is highly recommended!
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue

Which area(s) are affected? (Select all that apply)

Middleware, Runtime

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Vercel (Deployed), Other (Deployed)

Additional context

This has been fixed sometime during Next 15 development. Some of my apps are currently stuck on Next 14 (due to dependencies that are still incompatible with React 19), so it would be really nice to have this fix in Next 14.

@nphmuller nphmuller added the bug Issue was opened via the bug report template. label Nov 21, 2024
@github-actions github-actions bot added Middleware Related to Next.js Middleware. Runtime Related to Node.js or Edge Runtime with Next.js. labels Nov 21, 2024
@nphmuller
Copy link
Contributor Author

nphmuller commented Nov 21, 2024

The fix was first released in https://github.com/vercel/next.js/releases/tag/v15.0.0-canary.48

Very likely its: reject actions with invalid RSC responses: #67059

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. Middleware Related to Next.js Middleware. Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

1 participant