从服务器重定向后,NextJS 失败并显示 UND_ERR_REQ_CONTENT_LENGTH_MISMATCH

Rob*_*tos 6 reactjs next.js

这是我的第一个 NextJs 应用程序,我正在使用 serverActions 实验功能来管理表单提交并通过服务器端进行处理。

这是我的代码:

import { redirect } from "next/navigation";
import { cookies } from 'next/headers'
import { authService } from "@/services";

async function logoutUser() {
  "use server"
  
  authService.logout();
  cookies().delete('currentUser');
  redirect('/');
}

export default () => {
  return <>
    <form action={logoutUser} className="flex flex-col gap-3">
      <button className="btn btn-primary normal-case w-full" type="submit">Logout</button>
    </form>
  </>
}
Run Code Online (Sandbox Code Playgroud)

这里的想法是我想在用户注销后重定向回主页。

但是,每当代码执行时,我都会收到以下错误:

failed to get redirect response TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11) {
  cause: RequestContentLengthMismatchError: Request body length does not match content-length header
      at write (/Users/robison/Documents/Development/prototypes/watchlist-app/node_modules/next/dist/compiled/undici/index.js:1:67105)
      at _resume (/Users/robison/Documents/Development/prototypes/watchlist-app/node_modules/next/dist/compiled/undici/index.js:1:66726)
      at resume (/Users/robison/Documents/Development/prototypes/watchlist-app/node_modules/next/dist/compiled/undici/index.js:1:65413)
      at connect (/Users/robison/Documents/Development/prototypes/watchlist-app/node_modules/next/dist/compiled/undici/index.js:1:65301) {
    code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  }
}
Run Code Online (Sandbox Code Playgroud)

知道我做错了什么吗?

Naf*_*fis -1

您可以尝试使用startTransitionlogoutUser从其中调用。如果这不起作用,您可以尝试这篇文章,其中讨论了有关此确切问题的一些修复,该问题显然看起来像是与undici查看错误日志的模块相关的错误。