The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
`[Front-comp-renderer]` Host `componentSource` caching (#22958)
Paul Rastoin17d34a6fe3dc
diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/fetchComponentSource.spec.ts b/packages/twenty-front-component-renderer/src/host/utils/__tests__/fetchComponentSource.spec.tssimilarity index 83%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/fetchComponentSource.spec.tsrename to packages/twenty-front-component-renderer/src/host/utils/__tests__/fetchComponentSource.spec.tsindex 66ca4563451..303da49c419 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/fetchComponentSource.spec.ts+++ b/packages/twenty-front-component-renderer/src/host/utils/__tests__/fetchComponentSource.spec.ts@@ -1,7 +1,7 @@import { createHash, webcrypto } from 'node:crypto';import { TextEncoder as NodeTextEncoder } from 'node:util';-import { fetchComponentSource } from '@/remote/worker/utils/fetchComponentSource';+import { fetchComponentSource } from '@/host/utils/fetchComponentSource';const COMPONENT_SOURCE = 'export default () => {};';@@ -46,7 +46,13 @@ class FakeCache {},);- delete = jest.fn(async (key: string) => this.store.delete(key));+ delete = jest.fn(async (key: string | { url: string }) =>+ this.store.delete(typeof key === 'string' ? key : key.url),+ );++ keys = jest.fn(async () =>+ Array.from(this.store.keys()).map((key) => ({ url: key })),+ );}const isDefinedString = (value: string | undefined): value is string =>@@ -120,6 +126,42 @@ describe('fetchComponentSource', () => {expect(cache.put.mock.calls[0][0]).toBe(FINGERPRINTED_URL);});+ it('evicts stale entries of the same component when caching a new build', async () => {+ const cache = new FakeCache();++ const staleUrl = buildFingerprintedUrl(+ computeSha256Hex('previous build output'),+ );+ const otherComponentUrl =+ 'https://api.twenty.com/rest/front-components/other-component-id/0000000000000000000000000000000000000000000000000000000000000000.js';++ await cache.put(staleUrl, {+ text: async () => 'previous build output',+ });+ await cache.put(otherComponentUrl, {+ text: async () => 'other component source',+ });+ cache.put.mockClear();++ setupCaches(cache);++ const fetchMock = jest.fn(async () =>+ createFakeJsResponse(COMPONENT_SOURCE),+ );++ globalThis.fetch = fetchMock as unknown as typeof fetch;++ const source = await fetchComponentSource({ url: FINGERPRINTED_URL });++ // Eviction is fire-and-forget; flush pending promise callbacks+ await new Promise((resolve) => setTimeout(resolve, 0));++ expect(source).toBe(COMPONENT_SOURCE);+ expect(cache.delete).toHaveBeenCalledWith({ url: staleUrl });+ expect(cache.delete).not.toHaveBeenCalledWith({ url: otherComponentUrl });+ expect(cache.delete).not.toHaveBeenCalledWith({ url: FINGERPRINTED_URL });+ });+it('serves a verified cache hit without hitting the network', async () => {const cache = new FakeCache();diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/fetchComponentSourceFromNetwork.spec.ts b/packages/twenty-front-component-renderer/src/host/utils/__tests__/fetchComponentSourceFromNetwork.spec.tssimilarity index 93%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/fetchComponentSourceFromNetwork.spec.tsrename to packages/twenty-front-component-renderer/src/host/utils/__tests__/fetchComponentSourceFromNetwork.spec.tsindex 5b4733d1301..055e9a339f4 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/fetchComponentSourceFromNetwork.spec.ts+++ b/packages/twenty-front-component-renderer/src/host/utils/__tests__/fetchComponentSourceFromNetwork.spec.ts@@ -1,4 +1,4 @@-import { fetchComponentSourceFromNetwork } from '@/remote/worker/utils/fetchComponentSourceFromNetwork';+import { fetchComponentSourceFromNetwork } from '@/host/utils/fetchComponentSourceFromNetwork';const COMPONENT_URL ='https://api.twenty.com/rest/front-components/component-id/checksum-abc.js';@@ -55,6 +55,7 @@ describe('fetchComponentSourceFromNetwork', () => {expect(source).toBe('export default () => {};');expect(fetchMock).toHaveBeenCalledWith(CACHE_BUSTED_COMPONENT_URL, {headers: { Authorization: 'Bearer token' },+ credentials: 'omit',});});@@ -81,8 +82,11 @@ describe('fetchComponentSourceFromNetwork', () => {expect(source).toBe('presigned bundle source');expect(fetchMock).toHaveBeenNthCalledWith(1, CACHE_BUSTED_COMPONENT_URL, {headers: { Authorization: 'Bearer token' },+ credentials: 'omit',+ });+ expect(fetchMock).toHaveBeenNthCalledWith(2, PRESIGNED_URL, {+ credentials: 'omit',});- expect(fetchMock).toHaveBeenNthCalledWith(2, PRESIGNED_URL);});it('preserves existing query parameters when appending the cache bust parameter', async () => {@@ -98,7 +102,7 @@ describe('fetchComponentSourceFromNetwork', () => {expect(fetchMock).toHaveBeenCalledWith(`${COMPONENT_URL}?token=abc&cacheBust=v2`,- { headers: undefined },+ { headers: undefined, credentials: 'omit' },);});diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/extractComponentChecksumFromUrl.ts b/packages/twenty-front-component-renderer/src/host/utils/extractComponentChecksumFromUrl.tssimilarity index 100%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/extractComponentChecksumFromUrl.tsrename to packages/twenty-front-component-renderer/src/host/utils/extractComponentChecksumFromUrl.tsdiff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/fetchComponentSource.ts b/packages/twenty-front-component-renderer/src/host/utils/fetchComponentSource.tssimilarity index 77%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/fetchComponentSource.tsrename to packages/twenty-front-component-renderer/src/host/utils/fetchComponentSource.tsindex a2345c11d97..314696aa014 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/fetchComponentSource.ts+++ b/packages/twenty-front-component-renderer/src/host/utils/fetchComponentSource.ts@@ -1,8 +1,8 @@import { isDefined } from 'twenty-shared/utils';-import { extractComponentChecksumFromUrl } from '@/remote/worker/utils/extractComponentChecksumFromUrl';-import { fetchComponentSourceFromNetwork } from '@/remote/worker/utils/fetchComponentSourceFromNetwork';-import { frontComponentCacheStorageService } from '@/remote/worker/utils/frontComponentCacheStorageService';+import { extractComponentChecksumFromUrl } from '@/host/utils/extractComponentChecksumFromUrl';+import { fetchComponentSourceFromNetwork } from '@/host/utils/fetchComponentSourceFromNetwork';+import { frontComponentCacheStorageService } from '@/host/utils/frontComponentCacheStorageService';export const fetchComponentSource = async ({url,@@ -47,6 +47,10 @@ export const fetchComponentSource = async ({if (sourceChecksum === expectedChecksum) {frontComponentCacheStorageService.write({ cache, url, source });+ frontComponentCacheStorageService.evictStaleEntriesForComponent({+ cache,+ url,+ });}}diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/fetchComponentSourceFromNetwork.ts b/packages/twenty-front-component-renderer/src/host/utils/fetchComponentSourceFromNetwork.tssimilarity index 93%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/fetchComponentSourceFromNetwork.tsrename to packages/twenty-front-component-renderer/src/host/utils/fetchComponentSourceFromNetwork.tsindex 1d32fecfb39..e90775efc1e 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/fetchComponentSourceFromNetwork.ts+++ b/packages/twenty-front-component-renderer/src/host/utils/fetchComponentSourceFromNetwork.ts@@ -31,6 +31,7 @@ export const fetchComponentSourceFromNetwork = async ({}): Promise<string> => {const response = await fetch(appendCacheBustQueryParameter(url), {headers,+ credentials: 'omit',});if (!response.ok) {@@ -51,7 +52,9 @@ export const fetchComponentSourceFromNetwork = async ({throw new Error(`Invalid component source handoff response from ${url}`);}- const presignedResponse = await fetch(handoff.data.url);+ const presignedResponse = await fetch(handoff.data.url, {+ credentials: 'omit',+ });if (!presignedResponse.ok) {throw new Error(diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/frontComponentCacheStorageService.ts b/packages/twenty-front-component-renderer/src/host/utils/frontComponentCacheStorageService.tssimilarity index 73%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/frontComponentCacheStorageService.tsrename to packages/twenty-front-component-renderer/src/host/utils/frontComponentCacheStorageService.tsindex 242e9765570..cf83e4d158c 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/frontComponentCacheStorageService.ts+++ b/packages/twenty-front-component-renderer/src/host/utils/frontComponentCacheStorageService.ts@@ -78,4 +78,29 @@ export const frontComponentCacheStorageService = {delete: ({ cache, url }: { cache: Cache; url: string }): void => {cache.delete(url).catch(() => undefined);},++ evictStaleEntriesForComponent: ({+ cache,+ url,+ }: {+ cache: Cache;+ url: string;+ }): void => {+ const componentUrlPrefix = url.slice(0, url.lastIndexOf('/') + 1);++ cache+ .keys()+ .then((cachedRequests) => {+ const staleRequests = cachedRequests.filter(+ (cachedRequest) =>+ cachedRequest.url !== url &&+ cachedRequest.url.startsWith(componentUrlPrefix),+ );++ for (const staleRequest of staleRequests) {+ cache.delete(staleRequest).catch(() => undefined);+ }+ })+ .catch(() => undefined);+ },};diff --git a/packages/twenty-front-component-renderer/src/remote/components/FrontComponentWorkerEffect.tsx b/packages/twenty-front-component-renderer/src/remote/components/FrontComponentWorkerEffect.tsxindex 18c71c28aed..1b6d732d368 100644--- a/packages/twenty-front-component-renderer/src/remote/components/FrontComponentWorkerEffect.tsx+++ b/packages/twenty-front-component-renderer/src/remote/components/FrontComponentWorkerEffect.tsx@@ -5,11 +5,13 @@ import { useEffect, useRef } from 'react';import { buildHostFetchPolicyFromFrontComponentUrls } from '@/host/utils/buildHostFetchPolicyFromFrontComponentUrls';import { createFrontComponentHostThread } from '@/host/utils/createFrontComponentHostThread';import { createHostFetchEnforcingPolicy } from '@/host/utils/createHostFetchEnforcingPolicy';+import { fetchComponentSource } from '@/host/utils/fetchComponentSource';import { FRONT_COMPONENT_SANDBOX_DOCUMENT } from '@/remote/sandbox/generated/frontComponentSandboxDocument';import { createFrontComponentSandboxIframe } from '@/remote/sandbox/utils/createFrontComponentSandboxIframe';import { createFrontComponentSandboxMessageHandler } from '@/remote/sandbox/utils/createFrontComponentSandboxMessageHandler';import { type FrontComponentThread } from '@/types/FrontComponentThread';import { type SdkClientUrls } from '@/types/SdkClientUrls';+import { buildAuthorizationHeadersFromAccessToken } from '@/utils/buildAuthorizationHeadersFromAccessToken';type FrontComponentWorkerEffectProps = {componentUrl: string;@@ -71,24 +73,45 @@ export const FrontComponentWorkerEffect = ({setThread(thread);- thread.imports- .render(newReceiver.connection, {- componentUrl,- applicationAccessToken,- apiUrl,- functionsBaseUrl,- sdkClientUrls,- hostFetchOrigins: hostFetchPolicy.allowedOrigins,- applicationVariables,- })- .catch((error: Error) => {- setError(error);- });+ let isCancelled = false;++ const resolveComponentSourceAndRender = async () => {+ try {+ const componentSource = await fetchComponentSource({+ url: componentUrl,+ headers: buildAuthorizationHeadersFromAccessToken(+ applicationAccessToken,+ ),+ });++ if (isCancelled) {+ return;+ }++ await thread.imports.render(newReceiver.connection, {+ componentUrl,+ componentSource,+ applicationAccessToken,+ apiUrl,+ functionsBaseUrl,+ sdkClientUrls,+ hostFetchOrigins: hostFetchPolicy.allowedOrigins,+ applicationVariables,+ });+ } catch (error) {+ if (!isCancelled) {+ setError(error instanceof Error ? error : new Error(String(error)));+ }+ }+ };++ resolveComponentSourceAndRender();setReceiver(newReceiver);isInitializedRef.current = true;return () => {+ isCancelled = true;window.removeEventListener('message', handleSandboxMessage);setThread(null);channel.port1.close();diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/renderFrontComponent.test.ts b/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/renderFrontComponent.test.tsindex 04f16e23156..6f21166ecaf 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/renderFrontComponent.test.ts+++ b/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/renderFrontComponent.test.ts@@ -8,6 +8,7 @@ describe('renderFrontComponent', () => {connection: {} as RemoteConnection,renderContext: {componentUrl: 'https://api.twenty.test/rest/front-components/id',+ componentSource: 'export default () => {};',},hostFetch: null,}),diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/loadFrontComponentModule.ts b/packages/twenty-front-component-renderer/src/remote/worker/utils/loadFrontComponentModule.tsindex cf086f55d43..b00ad1bfd40 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/loadFrontComponentModule.ts+++ b/packages/twenty-front-component-renderer/src/remote/worker/utils/loadFrontComponentModule.ts@@ -1,16 +1,15 @@import { isDefined } from 'twenty-shared/utils';-import { buildAuthorizationHeadersFromAccessToken } from '@/remote/worker/utils/buildAuthorizationHeadersFromAccessToken';import { containsSdkClientImportSpecifier } from '@/remote/worker/utils/containsSdkClientImportSpecifier';import { createJavaScriptModuleBlobUrl } from '@/remote/worker/utils/createJavaScriptModuleBlobUrl';-import { fetchComponentSource } from '@/remote/worker/utils/fetchComponentSource';import { fetchSdkClientModulesAsBlobUrls } from '@/remote/worker/utils/fetchSdkClientModulesAsBlobUrls';import { revokeSdkClientModuleBlobUrls } from '@/remote/worker/utils/revokeSdkClientModuleBlobUrls';import { rewriteSdkClientImportsToBlobUrls } from '@/remote/worker/utils/rewriteSdkClientImportsToBlobUrls';import { type SdkClientUrls } from '@/types/SdkClientUrls';+import { buildAuthorizationHeadersFromAccessToken } from '@/utils/buildAuthorizationHeadersFromAccessToken';type LoadFrontComponentModuleInput = {- componentUrl: string;+ componentSource: string;sdkClientUrls?: SdkClientUrls;applicationAccessToken?: string;};@@ -20,7 +19,7 @@ type FrontComponentModule = {};export const loadFrontComponentModule = async ({- componentUrl,+ componentSource,sdkClientUrls,applicationAccessToken,}: LoadFrontComponentModuleInput): Promise<FrontComponentModule> => {@@ -28,11 +27,6 @@ export const loadFrontComponentModule = async ({applicationAccessToken,);- const componentSource = await fetchComponentSource({- url: componentUrl,- headers: authorizationHeaders,- });-const sdkModuleBlobUrls =isDefined(sdkClientUrls) &&containsSdkClientImportSpecifier(componentSource)diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/renderFrontComponent.ts b/packages/twenty-front-component-renderer/src/remote/worker/utils/renderFrontComponent.tsindex abe186f3640..386ebe77b63 100644--- a/packages/twenty-front-component-renderer/src/remote/worker/utils/renderFrontComponent.ts+++ b/packages/twenty-front-component-renderer/src/remote/worker/utils/renderFrontComponent.ts@@ -33,7 +33,7 @@ export const renderFrontComponent = async ({setWorkerEnvironmentVariablesFromRenderContext(renderContext);const componentModule = await loadFrontComponentModule({- componentUrl: renderContext.componentUrl,+ componentSource: renderContext.componentSource,sdkClientUrls: renderContext.sdkClientUrls,applicationAccessToken: renderContext.applicationAccessToken,});diff --git a/packages/twenty-front-component-renderer/src/types/HostToWorkerRenderContext.ts b/packages/twenty-front-component-renderer/src/types/HostToWorkerRenderContext.tsindex 5d76dc09aac..873d06aa7a9 100644--- a/packages/twenty-front-component-renderer/src/types/HostToWorkerRenderContext.ts+++ b/packages/twenty-front-component-renderer/src/types/HostToWorkerRenderContext.ts@@ -2,6 +2,7 @@ import { type SdkClientUrls } from '@/types/SdkClientUrls';export type HostToWorkerRenderContext = {componentUrl: string;+ componentSource: string;applicationAccessToken?: string;apiUrl?: string;functionsBaseUrl?: string;diff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/buildAuthorizationHeadersFromAccessToken.test.ts b/packages/twenty-front-component-renderer/src/utils/__tests__/buildAuthorizationHeadersFromAccessToken.test.tssimilarity index 100%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/__tests__/buildAuthorizationHeadersFromAccessToken.test.tsrename to packages/twenty-front-component-renderer/src/utils/__tests__/buildAuthorizationHeadersFromAccessToken.test.tsdiff --git a/packages/twenty-front-component-renderer/src/remote/worker/utils/buildAuthorizationHeadersFromAccessToken.ts b/packages/twenty-front-component-renderer/src/utils/buildAuthorizationHeadersFromAccessToken.tssimilarity index 100%rename from packages/twenty-front-component-renderer/src/remote/worker/utils/buildAuthorizationHeadersFromAccessToken.tsrename to packages/twenty-front-component-renderer/src/utils/buildAuthorizationHeadersFromAccessToken.ts