跳到内容

serverActions

用于配置 Next.js 应用中服务器行为的选项。

allowedOrigins

允许调用服务器行为的额外安全来源域的列表。Next.js 将服务器行为请求的来源与主机域进行比较,确保它们匹配以防止 CSRF 攻击。如果未提供,则仅允许同源。

next.config.js
/** @type {import('next').NextConfig} */
 
module.exports = {
  experimental: {
    serverActions: {
      allowedOrigins: ['my-proxy.com', '*.my-proxy.com'],
    },
  },
}

bodySizeLimit

默认情况下,发送到服务器行为的请求体的最大大小为 1MB,以防止在解析大量数据时消耗过多的服务器资源,以及潜在的 DDoS 攻击。

但是,您可以使用 serverActions.bodySizeLimit 选项配置此限制。它可以接受字节数或 bytes 支持的任何字符串格式,例如 1000'500kb''3mb'

next.config.js
/** @type {import('next').NextConfig} */
 
module.exports = {
  experimental: {
    serverActions: {
      bodySizeLimit: '2mb',
    },
  },
}

启用服务器行为 (v13)

服务器行为在 Next.js 14 中成为稳定功能,默认情况下启用。但是,如果您使用的是早期版本的 Next.js,您可以通过将 experimental.serverActions 设置为 true 来启用它们。

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