Fix subscribe dropping every signup - #118
Conversation
Resend's contacts API takes `segments` as an array of objects, not ids. Sending `[id]` returns 422 "expected object, received string", so no contact was ever created. Both audiences are empty as a result. The 422 was invisible because the handler treated any non-5xx as success to avoid leaking whether an address was already subscribed. Resend upserts contacts (a repeat signup returns 201 with the same id), so there was never a duplicate to hide, and swallowing 4xx only hid the bug. Report failures instead; the client already renders the error. Also show the signup on the blog index, not just individual posts. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QnCscaFDaj6TaBahqxXGqb
There was a problem hiding this comment.
Sorry @kixelated, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
WalkthroughThe global layout now accepts explicit subscription flags from page props and frontmatter. It falls back to showing subscriptions for dated blog posts. The blog index explicitly enables the subscription form. Resend contact creation now sends the segment ID in an object and returns HTTP 502 for all non-OK responses. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
/api/subscribehas never created a contact. Both Resend audiences are empty, which is why no announcement has ever had anyone to go to.Root cause: Resend's
POST /contactstakessegmentsas an array of objects.worker/index.tssent an array of ids:Why it stayed hidden: the handler treated any non-5xx as success, deliberately, so a repeat signup would not reveal that an address was already on the list. But 422 is non-5xx, so the visitor saw "Thanks!" while the contact was discarded. The premise turns out to be wrong anyway: Resend upserts, and a duplicate signup returns 201 with the same contact id, so there was never a duplicate case to hide. A non-ok response now returns an error, which
subscribe.tsxalready renders.The API key and segment id were both fine. The 422 (rather than a 401) is itself evidence the key was valid and reaching Resend.
Changes
worker/index.ts: correctsegmentsshape; stop reporting failures as success.src/layouts/global.astro:subscribeprop / frontmatter flag, so a page can show the form without having adate.src/pages/blog/index.astro: opt the blog index in.Test plan
Verified against the real Resend API, then cleaned up. Both audiences are back to 0 contacts and the only contacts that ever existed were the test ones below, all deleted.
Request shape, same address and segment, only the shape differing:
segments: ["7bfda95d-…"]Invalid input: expected object, received stringsegments: [{"id": "7bfda95d-…"}]Duplicate behaviour, disproving the original comment: posting the same address twice returned 201 both times with the identical contact id.
End-to-end through the worker under
wrangler dev:{"ok":true}200, and the contact appeared in the Blog segment{"error":"invalid email"}400, no API callRESEND_SEGMENT_ID->{"error":"subscribe failed"}502, where the old code returned{"ok":true}200Form placement, from the built output:
/blogindex/watch,/demo,/publish,/boy,/source,/issues, 404bun run checkandjust buildclean.Note
Anyone who tried to subscribe before this is unrecoverable beyond what is in the Workers logs. The fix does not take effect until the next
just deploy live.(written by Opus 5)
🤖 Generated with Claude Code
https://claude.ai/code/session_01QnCscaFDaj6TaBahqxXGqb