serverActions

用於設定 Next.js 應用程式中 伺服器動作 (Server Actions) 行為的選項。

allowedOrigins

一個額外的安全來源網域清單,可從這些網域呼叫 伺服器動作 (Server Actions)。Next.js 會比較 伺服器動作 (Server Actions) 請求的來源與主機網域,確保它們相符以防止 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',
    },
  },
}

啟用 伺服器動作 (Server Actions) (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