You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js Server Actions执行位置、部署方式及配置疑问(App Router)

Next.js App Router 中 Server Actions 的执行位置与配置解析

针对你提出的三个问题,结合Vercel部署的实际观测结果,整理解答如下:

1. Server Actions 是否运行在 Next.js Node 服务器上?

在开发环境和自托管生产环境(比如通过next start启动的Node.js服务器)中,Server Actions确实运行在Next.js的Node.js服务器进程内。它属于服务器端执行逻辑,但和/api目录下的路由处理器完全独立,是与React Server Components(RSC)绑定的执行单元。

2. 部署到 Vercel 时,Server Actions 运行在 Node 服务器还是 Edge Functions?

部署到Vercel时,Server Actions会和它所属的React Server Component被一起打包成后缀为.rsc的函数(也就是你在Functions面板看到的index.rsc)。默认情况下,Vercel会将这类RSC函数自动部署为Edge Functions,而不是传统的Node.js服务器函数。这也是你观测到“整个页面组件函数而非仅Server Actions被提取为Edge函数”的原因——Server Actions是RSC的一部分,会跟随所在组件的运行时一起打包部署。

3. 该执行方式是否可配置?

执行方式完全可以配置,支持全局和局部两种维度的设置:

  • 全局配置:在next.config.js中通过runtime字段指定全局默认运行时,比如设置为nodejs后,所有RSC和Server Actions都会使用Node.js Runtime:
    /** @type {import('next').NextConfig} */
    const nextConfig = {
      runtime: 'nodejs'
    }
    module.exports = nextConfig
    
  • 局部配置:在单个Server Action所在的组件文件中,通过导出runtime变量来指定该组件及其中Server Actions的运行时,比如:
    // 指定当前组件及其中的Server Actions使用Node.js运行时
    export const runtime = 'nodejs'
    
    async function myServerAction() {
      // Server Action逻辑
    }
    
    也可以单独给某个Server Action指定运行时(需要Next.js 13.4+):
    import { unstable_noStore as noStore } from 'next/cache'
    
    const myServerAction = async () => {
      noStore()
      // Server Action逻辑
    }
    
    // 给该Action单独指定Edge运行时
    myServerAction.runtime = 'edge'
    

内容的提问来源于stack exchange,提问作者melanie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 03:18:22