Next.js SSR应用部署Azure静态Web应用失败:函数内容过大
解决Next.js SSR部署Azure Static Web Apps时函数内容过大问题
排查方向与解决方案
1. 清理冗余依赖与构建产物
- 检查
package.json中的dependencies,移除未使用的生产依赖;将仅用于开发的依赖移至devDependencies,避免构建时打包无关内容。 - 构建后手动清理
.next/standalone目录的冗余文件,可在构建脚本中添加自动化清理命令:rm -rf .next/standalone/node_modules/*/.git rm -rf .next/standalone/node_modules/*/docs rm -rf .next/standalone/node_modules/*/test
2. 优化Next.js独立构建配置
- 在
next.config.js中添加排除规则,过滤非必要文件:module.exports = { output: "standalone", experimental: { outputFileTracingExcludes: { "*": [ "node_modules/@types/**/*", "node_modules/*.md", "node_modules/*.txt", "node_modules/**/*.map", ], }, }, };
3. 调整Azure部署YAML配置
- 在部署脚本中增加构建后清理步骤,进一步精简函数包体积:
jobs: build_and_deploy_job: steps: - uses: actions/checkout@v3 - name: Build Next.js app run: | npm install npm run build # 移除冗余文件 rm -rf .next/standalone/node_modules/**/*.min.js.map rm -rf .next/standalone/node_modules/**/LICENSE
4. 分离大型静态资源
- 将图片、视频等大型静态资源移至
public目录,Azure会自动托管这些资源,无需将其打包进函数目录。
5. 启用Azure Functions部署压缩
- 在YAML中开启运行包模式,减少上传体积:
app_settings: - name: WEBSITE_RUN_FROM_PACKAGE value: 1
内容的提问来源于stack exchange,提问作者saud00
相关产品推荐
相关产品推荐

