feat(web): implement dynamic home page routing (#2101) #2140
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.
Upgrade apps/web into an authentication-aware router using Cloudflare Service Bindings. The root URL (/) now serves different content based on user auth state:
This creates a seamless GitHub-like experience where the home page intelligently adapts based on user authentication state.
Service Bindings Configuration
Added service bindings in wrangler.jsonc to connect workers:
Critical Configurations
Added
run_worker_first: ["/"]to assets config Forces worker execution for / before serving static index.html Without this, static assets would bypass authentication logic entirelySet
assets.binding: "ASSETS"for explicit static asset servingSet
main: "./worker.ts"as worker entrypointWorker Implementation (worker.ts)
Created Hono-based worker with priority-ordered routing:
PRIORITY 1 - API routes: Proxy /api/* → API_SERVICE Fixes 500 errors when app makes API calls
PRIORITY 2 - App routes: Proxy to APP_SERVICE
PRIORITY 3 - Home page (/) with auth-aware logic:
PRIORITY 4 - Wildcard: Serve marketing pages from ASSETS Ensures /about, /features, /pricing work normally
Supports both GET and HEAD methods for proper HTTP compliance All / responses include cache control headers to prevent auth-state mixing
Helper Functions (lib/auth-helpers.ts)
Extracted reusable authentication utilities:
setCacheHeaders(response)- Prevents CDN/browser from caching auth-dependent content Sets Cache-Control: private, no-store and Vary: Cookie headersextractSession(json)- Robust session extraction from Better Auth responses Handles multiple response formats from different plugins/wrappersCOOKIE_NAMES- Constants for Better Auth cookie names Supports both regular and __Secure- prefixed cookies for HTTPSType Definitions (types/env.d.ts)
Added comprehensive TypeScript interfaces:
Env- Cloudflare Workers environment bindings ASSETS, APP_SERVICE, API_SERVICE, ENVIRONMENTBetterAuthApiResponse- API response structure Handles varying response shapes from Better AuthBetterAuthSession- Session object structure Standard fields: id, userId, expiresAtAll interfaces exported for reusability across modules
Security & Performance
Security:
Performance:
Testing
Runtime testing requires wrangler dev or production deployment to verify service binding execution.
Files Changed