跳到主要內容

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