serverActions

用于配置 Next.js 应用中服务端操作 (Server Actions) 行为的选项。

allowedOrigins

允许调用服务端操作 (Server Actions) 的额外安全来源域名列表。Next.js 会将服务端操作请求的来源与主机域名进行比对,确保两者匹配以防止 CSRF 攻击。若未提供此配置,则仅允许同源请求。

next.config.js
/** @type {import('next').NextConfig} */

module.exports = {
  experimental: {
    serverActions: {
      allowedOrigins: ['my-proxy.com', '*.my-proxy.com'],
    },
  },
}

bodySizeLimit

默认情况下,发送至服务端操作 (Server Actions) 的请求体最大为 1MB,以防止解析大量数据消耗过多服务器资源及潜在的 DDoS 攻击。

您可通过 serverActions.bodySizeLimit 选项配置此限制。该值可接受字节数或 bytes 库支持的任何字符串格式,例如 1000'500kb''3mb'

next.config.js
/** @type {import('next').NextConfig} */

module.exports = {
  experimental: {
    serverActions: {
      bodySizeLimit: '2mb',
    },
  },
}

启用服务端操作 (v13)

服务端操作 (Server Actions) 在 Next.js 14 中成为稳定功能并默认启用。若您使用更早版本的 Next.js,可通过将 experimental.serverActions 设为 true 来启用该功能。

next.config.js
/** @type {import('next').NextConfig} */
const config = {
  experimental: {
    serverActions: true,
  },
}

module.exports = config

On this page