fix: support React Native (window without document)#223
Merged
Conversation
React Native defines `window` but not `document`, `localStorage`, or `window.location`, so the SDK's `typeof window` guards (which only distinguish Node/SSR from browser) passed and then touched globals that don't exist. This fixes three React Native blockers: - Import-time crash: the analytics shared-state factory runs eagerly at module load and called `getAnalyticsConfigFromUrlParams()`, which read `window.location.search`. Now guarded on `window.location`. - Construction-time crash: `createAnalyticsModule` synchronously reads `document.referrer` on init. Analytics is now disabled on React Native (returns a noop `track`/`cleanup`) via a new `isReactNative` flag. - Per-request crash: the axios interceptor read `window.location.href` guarded only by `typeof window`. Now guarded on `window.location`. Node/SSR analytics behavior is unchanged: `isReactNative` targets only the window-without-a-DOM case, so headless queue/flush still works. Adds tests/unit/react-native.test.ts covering import, construction, and noop analytics under a window-without-document environment. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
🚀 Package Preview Available!Install this PR's preview build with npm: npm i @base44-preview/sdk@0.8.36-pr.223.4029308Prefer not to change any import paths? Install using npm alias so your code still imports npm i "@base44/sdk@npm:@base44-preview/sdk@0.8.36-pr.223.4029308"Or add it to your {
"dependencies": {
"@base44/sdk": "npm:@base44-preview/sdk@0.8.36-pr.223.4029308"
}
}
Preview published to npm registry — try new features instantly! |
OmerKat
approved these changes
Jul 8, 2026
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.
Summary
Makes the SDK import, construct, and make requests on React Native. RN defines a limited
windowpolyfill but has nodocument,localStorage, orwindow.location— so the SDK'stypeof window === "undefined"guards (which only tell Node/SSR apart from browser) passed in RN and then touched globals that aren't there.Analytics is disabled on React Native for now (returns a noop module); the rest of the SDK works.
Blockers fixed
analytics.tsbuilds its shared state eagerly at module load, and the factory callsgetAnalyticsConfigFromUrlParams()→window.location.search. Threw the moment the SDK was imported in RN. Now guarded with!window.location.createAnalyticsModulerunstrackInitializationEventsynchronously, readingdocument.referrer(optional chaining doesn't save an undeclared global →ReferenceError). The module now early-returns a noop{ track, cleanup }on RN.window.location.hrefguarded only bytypeof window. Now guarded with&& window.location.Design note
Gating is on a new
isReactNative = !isNode && typeof document === "undefined"flag, not a plain "no document" check. The SDK's analytics runs headless in Node (queue + flush), and there are tests for it; gating ondocumentalone would have disabled Node analytics too.isReactNativetargets precisely the window-without-a-DOM case the existing guards miss.Known limitation
getAnalyticsSessionIdfalls back to a fresh UUID per call on RN (nolocalStorage), so the anonymous-visitor id isn't stable across requests there. It's try/catch-guarded so it won't crash, and authenticated flows use the token instead. A stable id on RN would need an injectableAsyncStorage-style persistence layer — out of scope here.Testing
tests/unit/react-native.test.ts(3 tests) stubs a window-without-documentenv, re-imports the SDK, and asserts import + construction +analytics.trackdon't throw (fails without the fixes).🤖 Generated with Claude Code