Skip to content

Fix registration update API crash: headers already sent#1977

Merged
kylemh merged 3 commits intomainfrom
fix/registration-update-headers-sent
Mar 1, 2026
Merged

Fix registration update API crash: headers already sent#1977
kylemh merged 3 commits intomainfrom
fix/registration-update-headers-sent

Conversation

@irvingpop
Copy link
Contributor

Summary

Fixes #1965

The PATCH /api/registration/update endpoint was crashing with "Cannot set headers after they are sent to the client" (Sentry FRONT-END-86, 9 occurrences).

Root cause

When a user completes the final registration step, the API clears the opCodeApplicantEmail cookie. But the multi-step form fires additional PATCH requests after that (e.g. rapid clicking). These arrive with no cookie, the Airtable lookup returns nothing, and the code hits a broken res.writeHead(404).json() path -- writeHead() flushes headers immediately, so the subsequent .json() (which sets Content-Type) throws. The catch block's res.status(500).json() fails for the same reason, producing the unhandled error.

Confirmed by tracing the actual Sentry events: the user had 4 successful 200 PATCHes (filling all fields, which cleared the cookie), followed by 7 consecutive 500s.

Changes

  • pages/api/registration/update.ts: Replace writeHead(404).json() with status(404).json(). Add early 401 return when cookie is missing. Clear stale cookie on 404.
  • components/Forms/UpdateProfileForm/UpdateProfileForm.tsx: Detect 404 from the update API and redirect to /join instead of showing a dead-end error.
  • .gitignore: Add tsconfig.tsbuildinfo and .mcp.json.

Test plan

  • Existing UpdateProfileForm snapshot test passes
  • Lint and typecheck pass
  • Verify: complete registration flow, confirm no errors on final step
  • Confirm FRONT-END-86 stops recurring after deploy

🤖 Generated with Claude Code

irvingpop and others added 3 commits February 28, 2026 09:24
Replace broken writeHead(404).json() with status(404).json(), clear stale
cookie on 404, and redirect user to /join to re-register.

Fixes #1965

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The cookie gets cleared on the final successful form step. If additional
PATCH requests arrive after that, email is undefined and the Airtable
lookup fails — this was the actual trigger for the writeHead crash.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 28, 2026

Deployment failed with the following error:

You don't have permission to create a Preview Deployment for this Vercel project: operation-code.

View Documentation: https://vercel.com/docs/accounts/team-members-and-roles

@github-actions
Copy link

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 84.71% 5021 / 5927
🔵 Statements 84.71% 5021 / 5927
🔵 Functions 90.28% 158 / 175
🔵 Branches 93.53% 579 / 619
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
components/Forms/UpdateProfileForm/UpdateProfileForm.tsx 40% 50% 25% 40% 15-38, 60-94, 97-102, 106
Generated in workflow #47 for commit 2b5f981 by the Vitest Coverage Report Action

Copy link
Member

@kylemh kylemh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dint review 🤷🏼‍♂️

@kylemh kylemh merged commit a21ec10 into main Mar 1, 2026
9 of 11 checks passed
@kylemh kylemh deleted the fix/registration-update-headers-sent branch March 1, 2026 12:21
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.

Error: Cannot set headers after they are sent to the client

2 participants