From 9c06ce03e4c02905a8aa9b24a72382583dae2f6a Mon Sep 17 00:00:00 2001 From: Darshan Date: Fri, 16 May 2025 18:50:17 +0530 Subject: [PATCH 1/4] update: for main cloud instance, make an additional check with hostname since the env will be cached by the browsers. --- src/lib/stores/sdk.ts | 4 ++-- src/lib/system.ts | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index e590f984b..4ed01d0b6 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -1,4 +1,4 @@ -import { VARS } from '$lib/system'; +import { isMultiRegionSupported, VARS } from '$lib/system'; import { Account, Assistant, @@ -42,7 +42,7 @@ export function getApiEndpoint(region?: string): string { const hostname = url.hostname; // If instance supports multi-region, add the region subdomain. - const subdomain = VARS.APPWRITE_MULTI_REGION ? getSubdomain(region) : ''; + const subdomain = isMultiRegionSupported() ? getSubdomain(region) : ''; return `${protocol}//${subdomain}${hostname}/v1`; } diff --git a/src/lib/system.ts b/src/lib/system.ts index 80f7c3a8d..e39b7504c 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -10,8 +10,7 @@ export const VARS = { CONSOLE_MODE: (env.PUBLIC_CONSOLE_MODE as Mode) ?? undefined, APPWRITE_ENDPOINT: env.PUBLIC_APPWRITE_ENDPOINT ?? undefined, GROWTH_ENDPOINT: env.PUBLIC_GROWTH_ENDPOINT ?? undefined, - PUBLIC_STRIPE_KEY: env.PUBLIC_STRIPE_KEY ?? undefined, - APPWRITE_MULTI_REGION: env.PUBLIC_APPWRITE_MULTI_REGION === 'true' + PUBLIC_STRIPE_KEY: env.PUBLIC_STRIPE_KEY ?? undefined }; export const ENV = { @@ -28,3 +27,16 @@ export const isDev = ENV.DEV; export const isProd = ENV.PROD; export const hasStripePublicKey = !!VARS.PUBLIC_STRIPE_KEY; export const GRACE_PERIOD_OVERRIDE = false; + +/** + * Returns true if multi-region is supported. + */ +export function isMultiRegionSupported(): boolean { + if (env.PUBLIC_APPWRITE_MULTI_REGION === 'true') return true; + + try { + return new URL(VARS.APPWRITE_ENDPOINT).hostname.endsWith('cloud.appwrite.io'); + } catch { + return false; + } +} From 295463aa371e60b77dd055fa8e2e4da34eeba5cb Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 16 May 2025 14:16:16 +0000 Subject: [PATCH 2/4] fix: multi region support for cached env files --- src/lib/system.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/system.ts b/src/lib/system.ts index e39b7504c..7187df329 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -1,5 +1,6 @@ import { env } from '$env/dynamic/public'; import { dev } from '$app/environment'; +import { getApiEndpoint } from '$lib/stores/sdk'; export const enum Mode { CLOUD = 'cloud', @@ -35,7 +36,8 @@ export function isMultiRegionSupported(): boolean { if (env.PUBLIC_APPWRITE_MULTI_REGION === 'true') return true; try { - return new URL(VARS.APPWRITE_ENDPOINT).hostname.endsWith('cloud.appwrite.io'); + const endpoint = getApiEndpoint(); + return new URL(endpoint).hostname.endsWith('cloud.appwrite.io'); } catch { return false; } From 99b20f41261888c547c106c0bb21754dac048f7c Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 16 May 2025 16:18:47 +0200 Subject: [PATCH 3/4] Simplify URL extraction in isMultiRegionSupported function --- src/lib/system.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/system.ts b/src/lib/system.ts index 7187df329..5c78c491f 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -36,8 +36,7 @@ export function isMultiRegionSupported(): boolean { if (env.PUBLIC_APPWRITE_MULTI_REGION === 'true') return true; try { - const endpoint = getApiEndpoint(); - return new URL(endpoint).hostname.endsWith('cloud.appwrite.io'); + return new URL(getApiEndpoint()).hostname.endsWith('cloud.appwrite.io'); } catch { return false; } From 030618196a08ea46376797d026c6f21dc8114e53 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 16 May 2025 16:31:39 +0200 Subject: [PATCH 4/4] Fix isMultiRegionSupported to accept URL parameter --- src/lib/stores/sdk.ts | 2 +- src/lib/system.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index 4ed01d0b6..bf463d972 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -42,7 +42,7 @@ export function getApiEndpoint(region?: string): string { const hostname = url.hostname; // If instance supports multi-region, add the region subdomain. - const subdomain = isMultiRegionSupported() ? getSubdomain(region) : ''; + const subdomain = isMultiRegionSupported(url) ? getSubdomain(region) : ''; return `${protocol}//${subdomain}${hostname}/v1`; } diff --git a/src/lib/system.ts b/src/lib/system.ts index 5c78c491f..a23558304 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -1,6 +1,5 @@ import { env } from '$env/dynamic/public'; import { dev } from '$app/environment'; -import { getApiEndpoint } from '$lib/stores/sdk'; export const enum Mode { CLOUD = 'cloud', @@ -32,11 +31,11 @@ export const GRACE_PERIOD_OVERRIDE = false; /** * Returns true if multi-region is supported. */ -export function isMultiRegionSupported(): boolean { +export function isMultiRegionSupported(url: URL): boolean { if (env.PUBLIC_APPWRITE_MULTI_REGION === 'true') return true; try { - return new URL(getApiEndpoint()).hostname.endsWith('cloud.appwrite.io'); + return url.hostname.endsWith('cloud.appwrite.io'); } catch { return false; }