Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add client side cache for status endpoint (#2159)
Leendert de Borst6aeee16c7ad2
diff --git a/apps/browser-extension/src/utils/WebApiService.ts b/apps/browser-extension/src/utils/WebApiService.tsindex b16bb448e..a57835b2e 100644--- a/apps/browser-extension/src/utils/WebApiService.ts+++ b/apps/browser-extension/src/utils/WebApiService.ts@@ -32,13 +32,23 @@ type TokenResponse = {refreshToken: string;}+/**+ * How long a fetched /v2/Status response is reused before hitting the network again.+ */+const STATUS_CACHE_TTL_MS = 1000;++/**+ * Process-wide (background service worker) cache of the last successful /v2/Status response, shared across all+ * WebApiService instances.+ */+let cachedStatus: { data: StatusResponseV2; timestamp: number } | null = null;+/*** Service class for interacting with the web API.*/export class WebApiService {/**- * Get the base URL for the API from settings. This client speaks the v2 API exclusively — every endpoint- * (vault, auth, email, favicon, ...) is resolved under the /v2/ prefix.+ * Get the base URL for the API from settings.*/private async getBaseUrl(): Promise<string> {const apiUrl = await this.getApiUrl();@@ -310,12 +320,20 @@ export class WebApiService {* Auth errors (ApiAuthError) are re-thrown to be handled appropriately (e.g., trigger logout).*/public async getStatus(): Promise<StatusResponseV2> {+ /*+ * Serve a very recent status from the shared cache to prevent multiple round-trips to the server.+ */+ if (cachedStatus && Date.now() - cachedStatus.timestamp < STATUS_CACHE_TTL_MS) {+ return cachedStatus.data;+ }+try {- const status = await this.get<StatusResponseV2>('Auth/status');+ const status = await this.get<StatusResponseV2>('Status');// Persist the server version so it can be shown on the settings page, also while offline.if (status.serverVersion && status.serverVersion !== '0.0.0') {await storage.setItem('local:serverVersion', status.serverVersion);}+ cachedStatus = { data: status, timestamp: Date.now() };return status;} catch (error) {/**@@ -330,7 +348,8 @@ export class WebApiService {clientVersionSupported: true,serverVersion: '0.0.0',manifestRevisions: [],- srpSalt: ''+ srpSalt: '',+ isMigrated: false};}}