Skip to content

Commit c143573

Browse files
syns2191evereqgreptile-apps[bot]
authored
fix: server web api url setting not applying on runtime process (#4109)
* fix: server web api url setting not applying on runtime process * Apply suggestion from @greptile-apps[bot] Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Signed-off-by: Ruslan Konviser <[email protected]> * fix: dynamic server url in desktop mode * fix: silent error * fix: remove unused code --------- Signed-off-by: Ruslan Konviser <[email protected]> Co-authored-by: Ruslan Konviser <[email protected]> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent b2bd382 commit c143573

File tree

6 files changed

+32
-37
lines changed

6 files changed

+32
-37
lines changed

.scripts/configure.electron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function updateWebConstant(setDesktopApp: boolean) {
3434
const [from, to] = setDesktopApp ? [envCheck, hardcoded] : [hardcoded, envCheck];
3535

3636
if (!fileContent.includes(from)) {
37-
throw new Error(`Expected content not found in ${filePath}`);
37+
throw Error(`Expected content not found in ${filePath}`);
3838
}
3939

4040
fileContent = fileContent.replace(from, to);

apps/web/core/services/client/api.service.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
32
import {
43
APPLICATION_LANGUAGES_CODE,
54
DEFAULT_APP_PATH,
6-
GAUZY_API_BASE_SERVER_URL,
7-
IS_DESKTOP_APP
5+
IS_DESKTOP_APP,
6+
GAUZY_API_BASE_SERVER_URL
87
} from '@/core/constants/config/constants';
98

109
export const getFallbackAPI = async () => {
@@ -19,7 +18,7 @@ import {
1918
getTenantIdCookie
2019
} from '@/core/lib/helpers/cookies';
2120
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
22-
import { APIConfig, desktopServerOverride } from './axios';
21+
import { APIConfig } from './axios';
2322
import { HttpLoggerAdapter } from '../logs/logger-adapter.service';
2423
import { Logger } from '../logs/logger.service';
2524
import { ApiErrorService } from './api-error.service';
@@ -135,6 +134,10 @@ export class APIService {
135134
};
136135
}
137136

137+
protected get desktopBaseURL() {
138+
return GAUZY_API_BASE_SERVER_URL.value;
139+
}
140+
138141
getConfig() {
139142
return this.config;
140143
}
@@ -320,6 +323,8 @@ export class APIService {
320323
this.httpLogger.logError(error);
321324
}
322325

326+
327+
323328
/**
324329
* Get active request statistics for debugging and monitoring
325330
*/
@@ -356,14 +361,11 @@ export class APIService {
356361
const tenantId = getTenantIdCookie();
357362
const organizationId = getOrganizationIdCookie();
358363
let baseURL: string | undefined = this.baseURL;
359-
360364
if (IS_DESKTOP_APP) {
361-
// dynamic api host while on desktop mode
362-
const runtimeConfig = await desktopServerOverride();
363-
baseURL = (runtimeConfig || GAUZY_API_BASE_SERVER_URL.value) as string;
365+
baseURL = this.desktopBaseURL;
364366
}
365-
366367
baseURL = baseURL ? `${baseURL}/api` : undefined;
368+
367369
if (this.config.directAPI) {
368370
this.axiosInstance.defaults.baseURL = baseURL;
369371
}

apps/web/core/services/client/axios.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {
22
APPLICATION_LANGUAGES_CODE,
33
DEFAULT_APP_PATH,
4-
GAUZY_API_BASE_SERVER_URL,
5-
IS_DESKTOP_APP
4+
GAUZY_API_BASE_SERVER_URL
65
} from '@/core/constants/config/constants';
76
import { getAccessTokenCookie, getOrganizationIdCookie, getTenantIdCookie } from '@/core/lib/helpers/cookies';
87
import { AxiosRequestConfig, AxiosResponse } from 'axios';
@@ -86,30 +85,12 @@ export const getAPIDirect = async (): Promise<APIService> => {
8685

8786
export type APIConfig = AxiosRequestConfig<any> & { tenantId?: string; directAPI?: boolean };
8887

89-
export async function desktopServerOverride() {
90-
if (typeof window !== 'undefined') {
91-
try {
92-
const serverConfig = await api.get<{ NEXT_PUBLIC_GAUZY_API_SERVER_URL: string }>('/desktop-server');
93-
94-
return serverConfig?.data?.NEXT_PUBLIC_GAUZY_API_SERVER_URL;
95-
} catch (error) {
96-
return GAUZY_API_BASE_SERVER_URL;
97-
}
98-
}
99-
return GAUZY_API_BASE_SERVER_URL;
100-
}
10188
async function apiConfig(config?: APIConfig) {
10289
const tenantId = getTenantIdCookie();
10390
const organizationId = getOrganizationIdCookie();
10491

10592
let baseURL: string | undefined = GAUZY_API_BASE_SERVER_URL.value;
10693

107-
if (IS_DESKTOP_APP) {
108-
// dynamic api host while on desktop mode
109-
const runtimeConfig = await desktopServerOverride();
110-
baseURL = (runtimeConfig || GAUZY_API_BASE_SERVER_URL.value) as string;
111-
}
112-
11394
baseURL = baseURL ? `${baseURL}/api` : undefined;
11495

11596
apiDirect.axiosInstance.defaults.baseURL = baseURL;
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { GAUZY_API_SERVER_URL, GAUZY_API_BASE_SERVER_URL } from '@/core/constants/config/constants';
1+
import { GAUZY_API_SERVER_URL, IS_DESKTOP_APP } from '@/core/constants/config/constants';
22
import { IServerRuntimeConfig } from '@/core/types/interfaces/common/runtime-server-config';
33

44
export function getDesktopConfig(): Partial<IServerRuntimeConfig> {
55
// Next.js 16: serverRuntimeConfig removed, use environment variables instead
6-
return {
7-
GAUZY_API_SERVER_URL: process.env.GAUZY_API_SERVER_URL || GAUZY_API_SERVER_URL,
8-
NEXT_PUBLIC_GAUZY_API_SERVER_URL: process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL || GAUZY_API_BASE_SERVER_URL.value
9-
};
6+
if (IS_DESKTOP_APP) {
7+
return {
8+
GAUZY_API_SERVER_URL: process.env.GAUZY_API_SERVER_URL || GAUZY_API_SERVER_URL
9+
};
10+
}
11+
return {};
1012
}

apps/web/env-config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@ export function loadNextPublicEnvs() {
6464
}
6565

6666
// Preload Some variables
67-
setNextPublicEnv(loadNextPublicEnvs());
67+
if (process.env.NEXT_PUBLIC_IS_DESKTOP_APP === 'true' && typeof window !== 'undefined' && !Object.keys(NEXT_PUBLIC_ENVS.value).length) {
68+
(async () => {
69+
const resp = await fetch('/api/desktop-server');
70+
if (resp.ok) {
71+
const serverConfig = await resp.json();
72+
setNextPublicEnv({ ...loadNextPublicEnvs(), ...{ NEXT_PUBLIC_GAUZY_API_SERVER_URL: serverConfig?.GAUZY_API_SERVER_URL } });
73+
}
74+
})();
75+
} else {
76+
setNextPublicEnv(loadNextPublicEnvs());
77+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"pack:server-web": "yarn run pack --desktop=server-web",
9696
"config:server-web": "yarn run config:electron -- --environment=prod --desktop=server-web",
9797
"dev:server-web": "cross-env NODE_ENV=production yarn prepare:config:server-web && yarn build:web:desktop && yarn run prepare:server-web && yarn copy:build:web && yarn run start:server-web",
98-
"build:web:desktop": "yarn config:electron:web:build -- --type=constant && cross-env NEXT_BUILD_OUTPUT_TYPE=standalone NEXT_SHARP_PATH=/tmp/node_modules/sharp yarn build:web",
98+
"build:web:desktop": "yarn config:electron:web:build -- --type=constant && cross-env NEXT_PUBLIC_IS_DESKTOP_APP=true NEXT_BUILD_OUTPUT_TYPE=standalone NEXT_SHARP_PATH=/tmp/node_modules/sharp yarn build:web",
9999
"copy:build:web": "yarn ts-node .scripts/copy-web-build.ts && yarn config:electron:web:build -- --type=server",
100100
"start:server-web": "cd apps/server-web && yarn run start",
101101
"postinstall:electron": "cd apps/server-web && yarn run postinstall",

0 commit comments

Comments
 (0)