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',
    },
  },
}

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