Skip to content

fix(pwa): wrap writePwaManifest in try-catch to prevent build failure - #4429

Closed
88lin wants to merge 2 commits into
notionnext-org:mainfrom
88lin:fix/pwa-manifest-error-handling
Closed

fix(pwa): wrap writePwaManifest in try-catch to prevent build failure#4429
88lin wants to merge 2 commits into
notionnext-org:mainfrom
88lin:fix/pwa-manifest-error-handling

Conversation

@88lin

@88lin 88lin commented Aug 14, 2026

Copy link
Copy Markdown
Member

问题

writePwaManifest 使用 fs.writeFileSync 直接写入 public/manifest.json,在只读文件系统或权限不足时会抛出异常并中断整个构建流程。PWA manifest 写入失败不应阻止站点构建。

修复方案

  • fs.writeFileSync 包裹在 try-catch 中,写入失败时仅打印警告而非崩溃
  • 新增 __tests__/lib/pwa.server.test.js,mock 只读文件系统验证不抛异常

测试

  • ✅ 244 测试全部通过(含新增 error handling 测试)
  • yarn build 成功

变更文件

文件 说明
lib/pwa.server.js try-catch 错误处理
__tests__/lib/pwa.server.test.js 只读文件系统回归测试

88lin added 2 commits August 14, 2026 08:44
fs.writeFileSync was called without error handling. When BUILD_MODE
is enabled and public/manifest.json is not writable (e.g. read-only
filesystem in build/export environments), the write failure would
throw and abort the site data conversion process.

Wrap the write logic in try-catch; on failure, log a warning with the
path and error details, then continue gracefully.

Note: This code path is guarded by BUILD_MODE and only runs during
build/export, not during normal SSR. The original report's claim of
'500 error on every page' was overstated — the primary risk is
build/export interruption, not runtime SSR failure.
Add tests that mock a read-only filesystem to verify:
- writePwaManifest does not throw when fs.writeFileSync fails
- warning contains the failure reason
- manifestWritten only set on successful write
- write skipped when BUILD_MODE !== 'true'
@vercel

vercel Bot commented Aug 14, 2026

Copy link
Copy Markdown

@88lin is attempting to deploy a commit to the tangly1024's projects Team on Vercel.

A member of the Team first needs to authorize it.

@88lin 88lin closed this Aug 14, 2026
@88lin

88lin commented Aug 14, 2026

Copy link
Copy Markdown
Member Author

关闭此 PR。

原因

经复核,当前 try-catch 实现不建议原样合并:

  1. try-catch 范围过宽:同时捕获了 buildPwaManifest()fs.writeFileSync()。如果 manifest 构造出错,错误被静默吞掉,构建"成功"但线上使用过期的 public/manifest.json,更难排查。

  2. 运行时不会触发writePwaManifestBUILD_MODE !== 'true' 时直接 return,Serverless 只读文件系统不会走到写入逻辑。

  3. 构建阶段应 fail-fast:构建时 buildPwaManifest 抛错说明有真实 bug,应让构建失败暴露问题。

建议

  • 如有真实 fs.writeFileSync 失败案例,应只捕获写入操作,不捕获 manifest 构造
  • 更好的架构是只在构建阶段生成 manifest,运行时不写 public/

@88lin
88lin deleted the fix/pwa-manifest-error-handling branch August 14, 2026 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant