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

使用Gradle Node插件打包项目时遇Static Worker异常退出问题

解决Gradle Node Plugin打包Spring Boot+Next.js时的Next.js构建超时问题

我尝试用Gradle Node Plugin将Spring Boot REST API和基于Yarn的Next.js前端项目打包在一起,配置的build.gradle文件如下:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.4'
    id 'io.spring.dependency-management' version '1.1.0'
    // Gradle frontend
    id "com.github.node-gradle.node" version "3.5.1"
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    gradlePluginPortal()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    // Frontend
    implementation 'com.github.node-gradle:gradle-node-plugin:3.5.1'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

node {
    version = '18.13.0'
    yarnVersion = '1.22.19'
    download = true
    nodeProjectDir = file("${project.projectDir}/src/frontend")
}

tasks.withType(NodeTask).configureEach {
    npmCommand = false
    yarnCommand = true
}

tasks.register('yarnInstall', YarnTask) {
    args = ['install']
}

tasks.register('yarnBuild', YarnTask) {
    args = ['run', 'build']
}

tasks.register('cleanFrontend', Delete) {
    delete 'src/frontend/.next'
    delete 'src/frontend/node_modules'
}

tasks.register('copyFrontend', Copy) {
    from 'src/frontend/.next'
    into 'src/main/resources/static'
}

clean.dependsOn(cleanFrontend)
yarnBuild.dependsOn(yarnInstall)
copyFrontend.dependsOn(yarnBuild)
processResources.dependsOn(copyFrontend)

tasks.named('test') {
    useJUnitPlatform()
}

apply plugin: 'com.github.node-gradle.node'

执行构建时出现以下错误:

2023-03-13T14:54:11.725+0500 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :yarnBuild
2023-03-13T14:57:43.440+0500 [QUIET] [system.out] info  - Compiled successfully                                       
2023-03-13T14:57:43.674+0500 [QUIET] [system.out] info  - Collecting page data...

2023-03-13T14:57:43.617+0500 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :yarnBuild 
2023-03-13T14:58:57.532+0500 [ERROR] [system.err] Static worker unexpectedly exited with code: null and signal: SIGTERM
2023-03-13T14:58:57.558+0500 [ERROR] [system.err] warn  - Restarted collecting page data for [object Object] because it took more than 60 seconds
2023-03-13T14:58:57.559+0500 [ERROR] [system.err] warn  - See more info here https://nextjs.org/docs/messages/static-page-generation-timeout

2023-03-13T14:58:58.089+0500 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger]                   
2023-03-13T14:58:58.089+0500 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :yarnBuild 
2023-03-13T14:59:58.704+0500 [ERROR] [system.err] Static worker unexpectedly exited with code: null and signal: SIGTERM
2023-03-13T14:59:58.705+0500 [ERROR] [system.err] Static worker unexpectedly exited with code: null and signal: SIGTERM
2023-03-13T14:59:58.709+0500 [ERROR] [system.err] Static worker unexpectedly exited with code: null and signal: SIGTERM
2023-03-13T14:59:58.713+0500 [ERROR] [system.err]                                                                       
2023-03-13T14:59:58.713+0500 [ERROR] [system.err] > Build error occurred                                               
2023-03-13T14:59:58.717+0500 [ERROR] [system.err] Error: Collecting page data for [object Object] is still timing out after 2 attempts. See more info here https://nextjs.org/docs/messages/page-data-collection-timeout
2023-03-13T14:59:58.717+0500 [ERROR] [system.err]     at onRestart (SpringProjects\demo\src\frontend\node_modules\next\dist\build\index.js:605:35)
2023-03-13T14:59:58.718+0500 [ERROR] [system.err]     at Worker.isPageStatic (SpringProjects\demo\src\frontend\node_modules\next\dist\lib\worker.js:70:40)
2023-03-13T14:59:58.718+0500 [ERROR] [system.err]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2023-03-13T14:59:58.718+0500 [ERROR] [system.err]     at async Span.traceAsyncFn (SpringProjects\demo\src\frontend\node_modules\next\dist\trace\trace.js:79:20)
2023-03-13T14:59:58.719+0500 [ERROR] [system.err]     at async SpringProjects\demo\src\frontend\node_modules\next\dist\build\index.js:737:52    
2023-03-13T14:59:58.719+0500 [ERROR] [system.err]     at async Span.traceAsyncFn (SpringProjects\demo\src\frontend\node_modules\next\dist\trace\trace.js:79:20)
2023-03-13T14:59:58.720+0500 [ERROR] [system.err]     at async Promise.all (index 4)
2023-03-13T14:59:58.720+0500 [ERROR] [system.err]     at async SpringProjects\demo\src\frontend\node_modules\next\dist\build\index.js:676:17    
2023-03-13T14:59:58.720+0500 [ERROR] [system.err]     at async Span.traceAsyncFn (SpringProjects\demo\src\frontend\node_modules\next\dist\trace\trace.js:79:20)
2023-03-13T14:59:58.720+0500 [ERROR] [system.err]     at async SpringProjects\demo\src\frontend\node_modules\next\dist\build\index.js:629:170   
2023-03-13T14:59:58.808+0500 [LIFECYCLE] [org.gradle.internal.resources.AbstractTrackedResourceLock]

问题原因

这个错误是Next.js在静态页面生成(SSG)阶段,收集页面数据的耗时超过了默认的60秒超时限制,导致Worker进程被终止;也可能是Gradle构建环境的内存限制或进程管控导致Node.js Worker被强制终止。

解决方案

1. 延长Next.js静态生成超时时间

在Next.js项目根目录的next.config.js中添加超时配置,根据实际需求调整时长:

module.exports = {
  staticPageGenerationTimeout: 120, // 单位:秒,可根据需要调整
};

2. 优化Gradle构建时的Node.js资源配置

在build.gradle的yarnBuild任务中增加Node.js内存限制的环境变量,避免内存不足导致Worker崩溃:

tasks.register('yarnBuild', YarnTask) {
    args = ['run', 'build']
    // 扩大Node.js的堆内存限制,根据机器配置调整
    environment 'NODE_OPTIONS', '--max-old-space-size=4096'
}

3. 修正build.gradle的冗余配置

原配置中存在两处冗余:

  • 已经通过plugins块引入了node插件,无需在dependencies中添加implementation 'com.github.node-gradle:gradle-node-plugin:3.5.1',删除该行;
  • 文件末尾重复应用了apply plugin: 'com.github.node-gradle.node',删除该行。

4. 优化前端页面的静态生成逻辑

如果页面的getStaticProps或getStaticPaths中存在耗时操作(比如大量数据请求、复杂计算):

  • 优化数据请求逻辑,减少不必要的请求或缓存请求结果;
  • 考虑改用增量静态再生(ISR)或服务端渲染(SSR)替代全静态生成,降低构建阶段的耗时。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 02:17:09