Title: feat: Cloudflare Workers SSR support — createServerClient + worker-safe analytics by netanelgilad · Pull Request #221 · base44/javascript-sdk · GitHub
Open Graph Title: feat: Cloudflare Workers SSR support — createServerClient + worker-safe analytics by netanelgilad · Pull Request #221 · base44/javascript-sdk
X Title: feat: Cloudflare Workers SSR support — createServerClient + worker-safe analytics by netanelgilad · Pull Request #221 · base44/javascript-sdk
Description: Cloudflare Workers SSR support Motivation Full-stack hosting POC: running app SSR on Cloudflare Workers for Platforms. Today the SDK cannot be safely instantiated inside a Worker request handler: createClient() unconditionally constructs the analytics module, whose heartbeat processor runs a raw setInterval. In Workers, setInterval at global scope throws, and inside a request it leaks past the request lifetime — and there was no config option to disable analytics. axios 1.x adapter auto-detection (xhr → http → fetch) can pick the wrong adapter under workerd. Auth tokens live only in localStorage, so SSR document requests carry no auth at all. Changes Worker-safe analytics (src/modules/analytics.ts): the analytics module is now a no-op outside a browser (typeof window === 'undefined') — no heartbeat interval, no processing timers, track() queues nothing. A new disableAnalytics?: boolean on CreateClientConfig skips analytics explicitly; the server client sets it automatically. Axios adapter passthrough (src/utils/axios-client.ts, src/client.ts): createAxiosClient accepts an optional adapter, exposed as an adapter field on CreateClientConfig and forwarded to all four internal axios clients. The server client forces adapter: 'fetch'. New createServerClient(options) (src/server.ts, exported from the main entry): builds a per-request client for SSR/edge runtimes. Each config value resolves in order: explicit option → env (BASE44_APP_ID, BASE44_API_URL, BASE44_SERVICE_TOKEN, BASE44_FUNCTIONS_VERSION) → the same Base44-* request headers createClientFromRequest reads (so it degrades gracefully behind the existing proxy); the user token resolves option → Authorization: Bearer header → base44_access_token cookie (internal dependency-free cookie parser handles quoted + URL-encoded values). Throws a clear error when no appId resolves or when the serverUrl isn't absolute; forces analytics off, the fetch adapter, and requiresAuth: false — no window/localStorage access anywhere in the path. Also propagates Base44-State like createClientFromRequest. Cookie mirror for browser token storage (src/utils/auth-utils.ts, src/modules/auth.ts): saveAccessToken now also sets a base44_access_token cookie (path=/; SameSite=Lax, plus Secure on https) so subsequent document requests carry the token to SSR; removeAccessToken clears it. auth.setToken() and auth.logout() (which write localStorage inline) mirror/clear the same cookie so login and logout flows stay consistent with SSR. All cookie writes are guarded on typeof document !== 'undefined'; localStorage behavior is byte-for-byte unchanged. Compatibility guarantees Deno backend-functions contract unchanged: createClientFromRequest and its Authorization / Base44-Service-Authorization / Base44-App-Id / Base44-Api-Url / Base44-Functions-Version / Base44-State header handling are untouched. No exports map added to package.json — consumers that deep-import (e.g. @base44/sdk/dist/utils/axios-client) keep working. createServerClient is exported from the main entry only. Browser behavior unchanged when analytics is enabled: same heartbeat, batching, throttling, visibility handling, and initialization event; the no-op path only activates without window or when disableAnalytics is set. Test evidence npm test (typecheck via tsc --noEmit + vitest): 19 files, 197 tests passed (was 15 files / ~171 before this PR). npm run lint (eslint): clean. npm run build (tsc): clean. New coverage: tests/unit/server-client.test.ts: full resolution precedence (options → env → headers), cookie fallback incl. quoted/URL-encoded values, Authorization-over-cookie priority, malformed-header handling, appId-missing throws, non-absolute serverUrl throws, fetch adapter forced on all four axios clients, Base44-State propagation, and a worker simulation proving no timers start and track() is a safe no-op without window. tests/unit/auth-utils-cookie.test.ts: cookie-string builders (attributes, Secure, encoding, custom name, clearing) plus save/remove mirror behavior in a simulated browser and SSR no-op guards. tests/unit/analytics.test.ts: updated to simulate browser globals (analytics is browser-only now) and extended with heartbeat-preserved-in-browser and disableAnalytics no-op tests, plus a no-window worker-safety test. Note: this PR triggers an automatic @base44-preview/sdk npm preview publish for easy testing in the Workers POC. 🤖 Generated with Claude Code https://claude.ai/code/session_01DvhQfqxACcq25XAQRpoSh9 Generated by Claude Code
Open Graph Description: Cloudflare Workers SSR support Motivation Full-stack hosting POC: running app SSR on Cloudflare Workers for Platforms. Today the SDK cannot be safely instantiated inside a Worker request handler: ...
X Description: Cloudflare Workers SSR support Motivation Full-stack hosting POC: running app SSR on Cloudflare Workers for Platforms. Today the SDK cannot be safely instantiated inside a Worker request handler: ...
Opengraph URL: https://github.com/base44/javascript-sdk/pull/221
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:ee9fc198-f6a2-a937-5a3f-5f2b30e7c5ea |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BD86:2198A9:3649EF:47BA9F:6A4C2534 |
| html-safe-nonce | ebe34920156da65787b8dbc329e4b4cdce5b352330ada3c460bc68d165785325 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDg2OjIxOThBOTozNjQ5RUY6NDdCQTlGOjZBNEMyNTM0IiwidmlzaXRvcl9pZCI6IjE5OTE2NDUzNTI3NjgwNTQ1ODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 14b46cf6038704d7a00cd4adadad471eb2fe4dca2b0fd4b3d06eba1e7f493028 |
| hovercard-subject-tag | pull_request:3997800241 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/base44/javascript-sdk/pull/221/files |
| twitter:image | https://avatars.githubusercontent.com/u/3474905?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/3474905?s=400&v=4 |
| og:image:alt | Cloudflare Workers SSR support Motivation Full-stack hosting POC: running app SSR on Cloudflare Workers for Platforms. Today the SDK cannot be safely instantiated inside a Worker request handler: ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1b6b16d04026f131a36d57e3b01d0f4d26a51800edf48bf5ed0256e0ac905511 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/base44/javascript-sdk git https://github.com/base44/javascript-sdk.git |
| octolytics-dimension-user_id | 234699998 |
| octolytics-dimension-user_login | base44 |
| octolytics-dimension-repository_id | 939374418 |
| octolytics-dimension-repository_nwo | base44/javascript-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 939374418 |
| octolytics-dimension-repository_network_root_nwo | base44/javascript-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9ee13484b32cf23e15fde191da4c9aa47d41201c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width