Skip to content

The open alternative to Salesforce, designed for AI.

Mint optimistic cache cursors the way the server does (#25855)

Félix Malfait299e2baa3b71
diff --git a/packages/twenty-front/src/modules/apollo/utils/__tests__/encodeCursor.test.ts b/packages/twenty-front/src/modules/apollo/utils/__tests__/encodeCursor.test.ts
index 617bb8ed5b2..7b7e22f73b7 100644
--- a/packages/twenty-front/src/modules/apollo/utils/__tests__/encodeCursor.test.ts
+++ b/packages/twenty-front/src/modules/apollo/utils/__tests__/encodeCursor.test.ts
@@ -48,6 +48,46 @@ describe('encodeCursor', () => {
expect(decoded).toEqual({ id: '123', position: 1 });
});
+ // The server mints its own cursors with Buffer.toString('base64url'), so the
+ // bytes are pinned rather than only the shape of the alphabet: for this
+ // payload the two alphabets coincide and only the padding differs
+ it('should emit the same bytes the server would for the same payload', () => {
+ const record: ObjectRecord = {
+ __typename: 'ObjectRecord',
+ id: '81285b87-91e3-48ea-82ee-256b379f83d9',
+ position: 0,
+ };
+
+ expect(encodeCursor(record)).toBe(
+ Buffer.from(
+ JSON.stringify({ position: record.position, id: record.id }),
+ 'utf-8',
+ ).toString('base64url'),
+ );
+ expect(encodeCursor(record)).toMatch(/^[A-Za-z0-9_-]+$/);
+ });
+
+ // Jest resolves 'buffer' to Node's builtin, which implements 'base64url';
+ // the browser build gets the polyfill, which throws on it. Without this the
+ // suite would stay green while every optimistic cache write broke in the app
+ it('should only use encodings the browser Buffer polyfill implements', async () => {
+ jest.resetModules();
+ jest.doMock('buffer', () => jest.requireActual('buffer/'));
+
+ const { encodeCursor: encodeCursorWithPolyfill } =
+ await import('@/apollo/utils/encodeCursor');
+ const record: ObjectRecord = {
+ __typename: 'ObjectRecord',
+ id: '123',
+ position: 1,
+ };
+
+ expect(encodeCursorWithPolyfill(record)).toBe(encodeCursor(record));
+
+ jest.dontMock('buffer');
+ jest.resetModules();
+ });
+
it('should throw an error if record does not have an id', () => {
const record = { position: 1 } as any;
diff --git a/packages/twenty-front/src/modules/apollo/utils/encodeCursor.ts b/packages/twenty-front/src/modules/apollo/utils/encodeCursor.ts
index 1a6e71f4418..e8f9d7b63d5 100644
--- a/packages/twenty-front/src/modules/apollo/utils/encodeCursor.ts
+++ b/packages/twenty-front/src/modules/apollo/utils/encodeCursor.ts
@@ -1,7 +1,7 @@
import { type ObjectRecord } from '@/object-record/types/ObjectRecord';
import { Buffer } from 'buffer';
-import { isDefined } from 'twenty-shared/utils';
+import { base64UrlEncode, isDefined } from 'twenty-shared/utils';
export const encodeCursor = (record: ObjectRecord) => {
if (!('id' in record) || !isDefined(record.id)) {
@@ -16,5 +16,5 @@ export const encodeCursor = (record: ObjectRecord) => {
id: record.id,
};
- return Buffer.from(JSON.stringify(payload), 'utf-8').toString('base64');
+ return base64UrlEncode(Buffer.from(JSON.stringify(payload), 'utf-8'));
};