A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(app): reuse the cached discovery document on any 304 (#3751)
Hampus350d8970ba30
diff --git a/fluxer_app/src/features/app/state/InstanceSnapshotStore.ts b/fluxer_app/src/features/app/state/InstanceSnapshotStore.tsindex a82643052..80a28f7e3 100644--- a/fluxer_app/src/features/app/state/InstanceSnapshotStore.ts+++ b/fluxer_app/src/features/app/state/InstanceSnapshotStore.ts@@ -456,22 +456,29 @@ class InstanceSnapshotStore {return null;}- private conditionalHeaders(apiEndpoint: string): Record<string, string> | undefined {+ private revalidationSource(apiEndpoint: string): {instanceKey: string; entry: InstanceSnapshotEntry} | null {if (!isReachableThroughDocumentTransport(apiEndpoint)) {- return undefined;+ return null;}const found = this.findBy((entry) => entry.snapshot.apiEndpoint === apiEndpoint);- if (found == null) {+ if (found == null || (found.entry.etag == null && found.entry.lastModified == null)) {+ return null;+ }+ return found;+ }++ private conditionalHeaders(entry: InstanceSnapshotEntry | undefined): Record<string, string> | undefined {+ if (entry === undefined) {return undefined;}const headers: Record<string, string> = {};- if (found.entry.etag != null) {- headers[HttpHeader.IF_NONE_MATCH] = found.entry.etag;+ if (entry.etag != null) {+ headers[HttpHeader.IF_NONE_MATCH] = entry.etag;}- if (found.entry.lastModified != null) {- headers[HttpHeader.IF_MODIFIED_SINCE] = found.entry.lastModified;+ if (entry.lastModified != null) {+ headers[HttpHeader.IF_MODIFIED_SINCE] = entry.lastModified;}- return Object.keys(headers).length > 0 ? headers : undefined;+ return headers;}private setEntry(instanceKey: string, entry: InstanceSnapshotEntry): void {@@ -516,22 +523,22 @@ class InstanceSnapshotStore {if (desktopApi != null) {return await this.discoverThroughDesktopRuntime(desktopApi, apiEndpoint, signal);}+ const revalidated = this.revalidationSource(apiEndpoint);const result = await fetchInstanceDiscovery({input: apiEndpoint,fetch: fetchDiscoveryWithPrebootHandoff,signal: signal ?? undefined,- conditionalHeaders: this.conditionalHeaders(apiEndpoint),+ conditionalHeaders: this.conditionalHeaders(revalidated?.entry),adoptServingOrigin: adoptsDocumentOrigin(apiEndpoint),});if (result.kind === 'not-modified') {- const found = this.findBy((entry) => entry.discoveryUrl === result.url);- if (found == null) {+ if (revalidated == null) {throw new Error(`${result.url} responded 304 with no cached discovery document`);}runInAction(() => {- this.setEntry(found.instanceKey, {...found.entry, fetchedAt: Date.now()});+ this.setEntry(revalidated.instanceKey, {...revalidated.entry, fetchedAt: Date.now()});});- return this.toResolution(found.instanceKey, found.entry);+ return this.toResolution(revalidated.instanceKey, revalidated.entry);}return this.storeSnapshot(runtimeSnapshotFromDiscovery(result.document), {discoveryUrl: result.url,