Skip to content

Commit a81c01e

Browse files
committed
perf: eliminate wasted API call, afterUpdate overhead, and sequential billing checks
- Remove unused projects.list() call fired on every console load (result was never consumed) - Replace afterUpdate with reactive headerAlert subscription to avoid per-render overhead - Parallelize billing checks with Promise.all instead of sequential awaits - Fix database.subscribe() memory leak by wrapping in onMount for proper cleanup
1 parent 8d62e6c commit a81c01e

2 files changed

Lines changed: 17 additions & 38 deletions

File tree

src/routes/(console)/+layout.svelte

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { database, checkForDatabaseBackupPolicies } from '$lib/stores/database';
99
import { newOrgModal, organization } from '$lib/stores/organization';
1010
import { wizard } from '$lib/stores/wizard';
11-
import { afterUpdate, onMount } from 'svelte';
11+
import { onMount } from 'svelte';
1212
import { loading } from '$routes/store';
1313
import { requestedMigration } from '../store';
1414
import Create from './createOrganization.svelte';
@@ -280,10 +280,12 @@
280280
}
281281
}
282282
283-
database.subscribe(async (database) => {
284-
if (!database || !page.params.region || !page.params.project) return;
285-
// the component checks `isCloud` internally.
286-
await checkForDatabaseBackupPolicies(page.params.region, page.params.project, database);
283+
onMount(() => {
284+
return database.subscribe(async (database) => {
285+
if (!database || !page.params.region || !page.params.project) return;
286+
// the component checks `isCloud` internally.
287+
await checkForDatabaseBackupPolicies(page.params.region, page.params.project, database);
288+
});
287289
});
288290
289291
let currentOrganizationId = null;
@@ -293,18 +295,21 @@
293295
if (isCloud) {
294296
currentOrganizationId = org.$id;
295297
checkForEnterpriseTrial(org);
296-
await checkForUsageLimit(org);
297298
checkForMarkedForDeletion(org);
298-
await checkForNewDevUpgradePro(org);
299299
300-
if (org?.billingPlanDetails.requiresPaymentMethod) {
301-
await paymentExpired(org);
302-
await checkPaymentAuthorizationRequired(org);
300+
const billingChecks: Promise<unknown>[] = [
301+
checkForUsageLimit(org),
302+
checkForNewDevUpgradePro(org)
303+
];
303304
305+
if (org?.billingPlanDetails.requiresPaymentMethod) {
306+
billingChecks.push(paymentExpired(org), checkPaymentAuthorizationRequired(org));
304307
if (org?.billingTrialDays) {
305308
calculateTrialDay(org);
306309
}
307310
}
311+
312+
await Promise.all(billingChecks);
308313
$activeHeaderAlert = headerAlert.getExcluding('impersonation');
309314
}
310315
}
@@ -317,9 +322,7 @@
317322
318323
$registerSearchers(orgSearcher, projectsSearcher);
319324
320-
afterUpdate(() => {
321-
$activeHeaderAlert = headerAlert.getExcluding('impersonation');
322-
});
325+
$: void $headerAlert, ($activeHeaderAlert = headerAlert.getExcluding('impersonation'));
323326
</script>
324327

325328
<CommandCenter />

src/routes/(console)/+layout.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isCloud } from '$lib/system';
33
import type { LayoutLoad } from './$types';
44
import type { Account } from '$lib/stores/user';
55
import { Dependencies } from '$lib/constants';
6-
import { Platform, Query, type Models } from '@appwrite.io/console';
6+
import { Platform, type Models } from '@appwrite.io/console';
77
import { makePlansMap } from '$lib/helpers/billing';
88
import { plansInfo as plansInfoStore } from '$lib/stores/billing';
99
import { normalizeConsoleVariables } from '$lib/helpers/domains';
@@ -54,7 +54,6 @@ export const load: LayoutLoad = async ({ depends, parent, url }) => {
5454
currentOrgId: undefined,
5555
organizations,
5656
consoleVariables,
57-
allProjectsCount: 0,
5857
plansInfo: plansInfo ?? null,
5958
version: versionData?.version ?? null
6059
};
@@ -107,28 +106,6 @@ export const load: LayoutLoad = async ({ depends, parent, url }) => {
107106
preferences.organization ??
108107
(organizations.teams.length > 0 ? organizations.teams[0].$id : undefined);
109108

110-
// Load projects for the current organization if one is selected
111-
let projectsCount = 0;
112-
if (currentOrgId) {
113-
try {
114-
projectsCount = (
115-
await sdk.forConsole.projects.list({
116-
queries: [
117-
Query.equal('teamId', currentOrgId),
118-
Query.limit(1),
119-
Query.select(['$id'])
120-
]
121-
})
122-
).total;
123-
} catch (e) {
124-
if (shouldRedirectToVerifyEmail(e)) {
125-
redirect(303, verifyEmailUrl);
126-
}
127-
128-
projectsCount = 0;
129-
}
130-
}
131-
132109
// just in case!
133110
plansInfoStore.set(fallbackPlansInfoArray);
134111

@@ -139,7 +116,6 @@ export const load: LayoutLoad = async ({ depends, parent, url }) => {
139116
currentOrgId,
140117
organizations,
141118
consoleVariables,
142-
allProjectsCount: projectsCount,
143119
plansInfo: fallbackPlansInfoArray,
144120
version: versionData?.version ?? null
145121
};

0 commit comments

Comments
 (0)