Skip to content

The open alternative to Salesforce, designed for AI.

fix: preserve API path prefixes in CSV export downloads (#26568)

Weiko82cdce878867
diff --git a/packages/twenty-front/src/modules/record-export/utils/__tests__/createRecordExportConnection.test.ts b/packages/twenty-front/src/modules/record-export/utils/__tests__/createRecordExportConnection.test.ts
index 7cb915cc38a..0dfe7b5f233 100644
--- a/packages/twenty-front/src/modules/record-export/utils/__tests__/createRecordExportConnection.test.ts
+++ b/packages/twenty-front/src/modules/record-export/utils/__tests__/createRecordExportConnection.test.ts
@@ -75,6 +75,8 @@ describe('createRecordExportConnection', () => {
'https://workspace.example.test',
'https://custom-domain.example.test',
'http://localhost:3000',
+ 'https://workspace.example.test/twenty',
+ 'http://localhost:3000/apps/twenty',
])('downloads through the authenticated API at %s', async (serverBaseUrl) => {
mockServerBaseUrl = serverBaseUrl;
const click = jest
@@ -102,6 +104,43 @@ describe('createRecordExportConnection', () => {
expect(click.mock.instances[0]).toHaveAttribute('download', 'person.csv');
});
+ it.each([
+ 'https://api.example.test/file/record-export/export',
+ 'https://api.example.test/twenty/file/record-export/export',
+ 'https://api.example.test/canonical/file/record-export/export',
+ '/file/record-export/export',
+ '/twenty/file/record-export/export',
+ 'file/record-export/export',
+ ])(
+ 'preserves the API base path when downloading %s',
+ async (downloadPath) => {
+ mockServerBaseUrl = 'https://workspace.example.test/twenty/';
+ const click = jest
+ .spyOn(HTMLAnchorElement.prototype, 'click')
+ .mockImplementation(() => {});
+ const finished = createRecordExportConnection().exportRecords({ input });
+
+ expect(createClient).toHaveBeenCalledWith(
+ expect.objectContaining({
+ url: 'https://workspace.example.test/twenty/metadata',
+ credentials: 'include',
+ }),
+ );
+ update({
+ progress: 100,
+ downloadUrl: `${downloadPath}?token=signed%2Btoken%2Fvalue%3D&expires=123`,
+ });
+
+ await expect(finished).resolves.toBeUndefined();
+ expect(click).toHaveBeenCalledTimes(1);
+ expect(click.mock.instances[0]).toHaveAttribute(
+ 'href',
+ 'https://workspace.example.test/twenty/file/record-export/export?token=signed%2Btoken%2Fvalue%3D&expires=123',
+ );
+ expect(click.mock.instances[0]).toHaveAttribute('download', 'person.csv');
+ },
+ );
+
it('surfaces the server failure and closes the connection', async () => {
const finished = createRecordExportConnection().exportRecords({ input });
update({ errorMessage: 'Export failed' });
diff --git a/packages/twenty-front/src/modules/record-export/utils/createRecordExportConnection.ts b/packages/twenty-front/src/modules/record-export/utils/createRecordExportConnection.ts
index 7a25880212e..1f6a34d0d9a 100644
--- a/packages/twenty-front/src/modules/record-export/utils/createRecordExportConnection.ts
+++ b/packages/twenty-front/src/modules/record-export/utils/createRecordExportConnection.ts
@@ -10,6 +10,7 @@ import {
} from '~/generated-metadata/graphql';
export const createRecordExportConnection = () => {
+ const serverBaseUrl = REACT_APP_SERVER_BASE_URL.replace(/\/$/, '');
let cancel: (() => void) | undefined;
const exportRecords = ({
@@ -22,7 +23,7 @@ export const createRecordExportConnection = () => {
cancel?.();
onProgress?.(0);
const client = createClient({
- url: `${REACT_APP_SERVER_BASE_URL}/metadata`,
+ url: `${serverBaseUrl}/metadata`,
credentials: 'include',
retryAttempts: 0,
});
@@ -64,12 +65,13 @@ export const createRecordExportConnection = () => {
}
onProgress?.(recordExport.progress);
if (isDefined(recordExport.downloadUrl)) {
- const { pathname, search } = new URL(
+ const { search } = new URL(
recordExport.downloadUrl,
- REACT_APP_SERVER_BASE_URL,
+ serverBaseUrl,
+ );
+ const downloadUrl = new URL(
+ `${serverBaseUrl}/file/record-export/${recordExport.id}`,
);
- const downloadUrl = new URL(REACT_APP_SERVER_BASE_URL);
- downloadUrl.pathname = pathname;
downloadUrl.search = search;
const link = document.createElement('a');
link.href = downloadUrl.toString();