fix(worker): serve API same-origin via Cloudflare Pages Functions (GOAP-252) - #1022
Merged
Conversation
The frontend build runs on Cloudflare Pages directly, so the Worker API can ride the same deployment through a functions/api catch-all that re-serves the Hono app — no separate Worker deploy, no credentials, no CORS, no VITE_API_BASE_URL. Drops the standalone wrangler deploy from the release workflow and points its health check at the Pages origin. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
|
✅ Health of changed files: 10.0 ✅ Health gate: passed 📊 See the full report for this PR |
Deploying do-epub-studio with
|
| Latest commit: |
520674d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://c72a9935.do-epub-studio.pages.dev |
| Branch Preview URL: | https://fix-pages-functions-api.do-epub-studio.pages.dev |
Contributor
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
The frontend build runs on Cloudflare Pages directly (Pages Git integration, root
wrangler.toml). So instead of deploying the Worker separately (which needs Cloudflare credentials, a separate origin, CORS, andVITE_API_BASE_URL), the API now rides the same Pages deployment via a Pages Function — same origin, zero credentials, no frontend change needed (the web app's production default already targets same-origin/api/*).This is ADR-252's "preferred long-term" option, now realized.
1.
functions/api/[[path]].ts(new)A Pages Function catch-all that re-serves the existing Worker's Hono app for all
/api/*requests:Only
/api/*matches — every other path falls through to the static SPA untouched.Verified locally:
wrangler pages functions buildcompiles it into a ~2MB bundle (full Hono app)wrangler pages dev apps/web/dist→GET /api/healthreturns{"ok":true}JSON 200,/still serves the SPA, andPOST /api/access/requestreturns a proper JSON error envelope (not the HTML fallback that caused "Invalid server response")2.
release.yml— no standalone Worker deploy, no credentialsPR #1018 had restored a
wrangler deploystep + workers.dev health check. With the Pages Functions model that's wrong (needsCLOUDFLARE_API_TOKEN, checks the wrong URL). Removed the deploy/secrets steps; the post-deploy health check now assertshttps://do-epub-studio.pages.dev/api/healthreturns200+{"ok":true}(fail-closed, ADR-187).3. Docs + plans
docs/runbooks/infrastructure-setup.mdrewritten for the Pages Functions model: dashboard bindings (D1DB, R2BOOKS_BUCKET, KVCACHE_KV) + vars, noVITE_API_BASE_URL, nowrangler deploy.Notes on Pages constraints (all degrade gracefully, login unaffected)
RATE_LIMITER) can't be created inside a Pages project → rate limiting fails open (documentedrate-limit-client.tsbehavior).Remaining (operator, dashboard-only, no code)
Create D1/R2/KV, apply migrations, bind
DB/BOOKS_BUCKET/CACHE_KV+ vars (APP_BASE_URL,ENVIRONMENT,WEBAUTHN_*, signing secrets) on the Pages project, seed data, push → Pages builds frontend + functions → login works.Verification
QUALITY_GATE_NO_SMOKE=1) passes./scripts/validate-workflows.sh— all 11 workflows passnode scripts/check-adr-index.mjspasseswrangler pages functions build+wrangler pages devverified locally