Skip to content

fix(worker): serve API same-origin via Cloudflare Pages Functions (GOAP-252) - #1022

Merged
d-o-hub merged 1 commit into
mainfrom
fix/pages-functions-api
Aug 21, 2026
Merged

fix(worker): serve API same-origin via Cloudflare Pages Functions (GOAP-252)#1022
d-o-hub merged 1 commit into
mainfrom
fix/pages-functions-api

Conversation

@d-o-hub

@d-o-hub d-o-hub commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

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, and VITE_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:

export function onRequest(context) {
  return app.fetch(context.request, context.env, context.ctx);
}

Only /api/* matches — every other path falls through to the static SPA untouched.

Verified locally:

  • wrangler pages functions build compiles it into a ~2MB bundle (full Hono app)
  • wrangler pages dev apps/web/distGET /api/health returns {"ok":true} JSON 200, / still serves the SPA, and POST /api/access/request returns a proper JSON error envelope (not the HTML fallback that caused "Invalid server response")

2. release.yml — no standalone Worker deploy, no credentials

PR #1018 had restored a wrangler deploy step + workers.dev health check. With the Pages Functions model that's wrong (needs CLOUDFLARE_API_TOKEN, checks the wrong URL). Removed the deploy/secrets steps; the post-deploy health check now asserts https://do-epub-studio.pages.dev/api/health returns 200 + {"ok":true} (fail-closed, ADR-187).

3. Docs + plans

  • docs/runbooks/infrastructure-setup.md rewritten for the Pages Functions model: dashboard bindings (D1 DB, R2 BOOKS_BUCKET, KV CACHE_KV) + vars, no VITE_API_BASE_URL, no wrangler deploy.
  • GOAP-252 + ADR-252 updated to record the same-origin decision.
  • LEARNINGS captured.

Notes on Pages constraints (all degrade gracefully, login unaffected)

  • Durable Object (RATE_LIMITER) can't be created inside a Pages project → rate limiting fails open (documented rate-limit-client.ts behavior).
  • Email Sending isn't a Pages Function binding → recovery email falls back to logging.

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

  • Full quality gate (QUALITY_GATE_NO_SMOKE=1) passes
  • ./scripts/validate-workflows.sh — all 11 workflows pass
  • node scripts/check-adr-index.mjs passes
  • wrangler pages functions build + wrangler pages dev verified locally

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>
@d-o-hub
d-o-hub requested a review from d-oit as a code owner August 21, 2026 07:19
@repowise-bot

repowise-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

✅ Health of changed files: 10.0
🚨 Change risk: high, riskier than 77% of this repo's commits.

✅ Health gate: passed


📊 See the full report for this PR
Your repo map with this PR's blast radius lit up, every caller of the contracts it changes, and health before and after. No sign-in. · ⭐ Star Repowise · 📥 Install bot · Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot · Updated 2026-08-21 07:19 UTC

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying do-epub-studio with  Cloudflare Pages  Cloudflare Pages

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

View logs

@github-actions github-actions Bot added documentation Improvements or additions to documentation ci agents labels Aug 21, 2026
@codacy-production

Copy link
Copy Markdown
Contributor

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

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.

@d-o-hub
d-o-hub merged commit 87a0060 into main Aug 21, 2026
27 checks passed
@d-o-hub
d-o-hub deleted the fix/pages-functions-api branch August 21, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents ci documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants