diff --git a/.changeset/tough-files-leave.md b/.changeset/tough-files-leave.md new file mode 100644 index 0000000000..393bdd2cd0 --- /dev/null +++ b/.changeset/tough-files-leave.md @@ -0,0 +1,5 @@ +--- +"@uploadthing/expo": patch +--- + +Fix URL construction - Previously the code checked if window.location is defined, which threw an error because window did not exist, this caused it to fallback to the default URL without checking the env variable The new code will check if the env variable FIRST and use it if it exists, then check safely for the window and then for the debuggerHost diff --git a/packages/expo/src/index.ts b/packages/expo/src/index.ts index d1ff6a81bc..1518790997 100644 --- a/packages/expo/src/index.ts +++ b/packages/expo/src/index.ts @@ -16,9 +16,9 @@ export interface GenerateTypedHelpersOptions { * @example "/api/uploadthing" * @example "https://www.example.com/api/uploadthing" * - * If relative, host will be inferred from either the `EXPO_PUBLIC_SERVER_ORIGIN` environment variable or `ExpoConstants.hostUri` + * If relative, host will be inferred from either the `EXPO_PUBLIC_SERVER_URL` environment variable or `ExpoConstants.hostUri` * - * @default (process.env.EXPO_PUBLIC_SERVER_ORIGIN ?? ExpoConstants.debuggerHost) + "/api/uploadthing" + * @default (process.env.EXPO_PUBLIC_SERVER_URL ?? ExpoConstants.debuggerHost) + "/api/uploadthing" */ url?: URL | string; /** @@ -55,17 +55,18 @@ export const generateReactNativeHelpers = ( let url = new URL("http://localhost:8081/api/uploadthing"); try { url = new URL( - initOpts?.url ?? "/api/uploadthing", - typeof window.location !== "undefined" - ? window.location.origin - : (process.env.EXPO_PUBLIC_SERVER_ORIGIN ?? `http://${debuggerHost}`), + initOpts?.url ?? "/api/uploadthing", + process.env.EXPO_PUBLIC_SERVER_URL ?? + (typeof window !== "undefined" && window.location?.origin + ? window.location?.origin + : `http://${debuggerHost}`), ); } catch (err) { // Can't throw since window.location is undefined in Metro pass // but may get defined when app mounts. // eslint-disable-next-line no-console console.warn( - `Failed to resolve URL from ${initOpts?.url?.toString()} and ${process.env.EXPO_PUBLIC_SERVER_ORIGIN} or ${debuggerHost}. Your application may not work as expected.`, + `Failed to resolve URL from ${initOpts?.url?.toString()} and ${process.env.EXPO_PUBLIC_SERVER_URL} or ${debuggerHost}. Your application may not work as expected.`, err, ); }