跳至內容
API 參考next.config.js 選項serverActions (伺服器動作)

伺服器動作 (Server Actions)

在 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.config.js
/** @type {import('next').NextConfig} */
const config = {
  experimental: {
    serverActions: true,
  },
}
 
module.exports = config