diff --git a/app/client/src/actions/pageActions.tsx b/app/client/src/actions/pageActions.tsx index 914f5d83e7a6..c998d7546e2b 100644 --- a/app/client/src/actions/pageActions.tsx +++ b/app/client/src/actions/pageActions.tsx @@ -73,6 +73,7 @@ export interface FetchPublishedPageActionPayload { export interface FetchPublishedPageResourcesPayload { pageId: string; basePageId: string; + branch: string; } export const fetchPublishedPageAction = ( @@ -298,12 +299,14 @@ export const clonePageSuccess = ({ // In future we can reuse this for fetching other page level resources in published mode export const fetchPublishedPageResources = ({ basePageId, + branch, pageId, }: FetchPublishedPageResourcesPayload): ReduxAction => ({ type: ReduxActionTypes.FETCH_PUBLISHED_PAGE_RESOURCES_INIT, payload: { pageId, basePageId, + branch, }, }); diff --git a/app/client/src/api/services/ConsolidatedPageLoadApi/api.ts b/app/client/src/api/services/ConsolidatedPageLoadApi/api.ts index 4772853c901e..308da2415f38 100644 --- a/app/client/src/api/services/ConsolidatedPageLoadApi/api.ts +++ b/app/client/src/api/services/ConsolidatedPageLoadApi/api.ts @@ -1,20 +1,20 @@ import { api } from "api/core"; import type { InitConsolidatedApi } from "sagas/InitSagas"; +import type { ConsolidatedApiParams } from "./types"; +import { ConsolidatedApiUtils } from "./url"; -const BASE_URL = "v1/consolidated-api"; -const VIEW_URL = `${BASE_URL}/view`; -const EDIT_URL = `${BASE_URL}/edit`; +export const getConsolidatedPageLoadDataView = async ( + params: ConsolidatedApiParams, +) => { + const viewUrl = ConsolidatedApiUtils.getViewUrl(params); -export const getConsolidatedPageLoadDataView = async (params: { - applicationId?: string; - defaultPageId?: string; -}) => { - return api.get(VIEW_URL, { params }); + return api.get(viewUrl); }; -export const getConsolidatedPageLoadDataEdit = async (params: { - applicationId?: string; - defaultPageId?: string; -}) => { - return api.get(EDIT_URL, { params }); +export const getConsolidatedPageLoadDataEdit = async ( + params: ConsolidatedApiParams, +) => { + const editUrl = ConsolidatedApiUtils.getEditUrl(params); + + return api.get(editUrl); }; diff --git a/app/client/src/api/services/ConsolidatedPageLoadApi/types.ts b/app/client/src/api/services/ConsolidatedPageLoadApi/types.ts new file mode 100644 index 000000000000..5a8deb86d891 --- /dev/null +++ b/app/client/src/api/services/ConsolidatedPageLoadApi/types.ts @@ -0,0 +1,5 @@ +export interface ConsolidatedApiParams { + applicationId?: string; + defaultPageId?: string; + branchName?: string; +} diff --git a/app/client/src/api/services/ConsolidatedPageLoadApi/url.ts b/app/client/src/api/services/ConsolidatedPageLoadApi/url.ts new file mode 100644 index 000000000000..626f890779c3 --- /dev/null +++ b/app/client/src/api/services/ConsolidatedPageLoadApi/url.ts @@ -0,0 +1,25 @@ +import pickBy from "lodash/pickBy"; +import identity from "lodash/identity"; +import type { ConsolidatedApiParams } from "./types"; + +export class ConsolidatedApiUtils { + private static BASE_URL = "v1/consolidated-api"; + private static VIEW_URL = `${this.BASE_URL}/view`; + private static EDIT_URL = `${this.BASE_URL}/edit`; + + static getViewUrl = (requestParams: ConsolidatedApiParams) => { + // Remove undefined values from the requestParams object + const queryParamsObject = pickBy(requestParams, identity); + const queryParamsString = new URLSearchParams(queryParamsObject).toString(); + + return `${this.VIEW_URL}?${queryParamsString}`; + }; + + static getEditUrl = (requestParams: ConsolidatedApiParams) => { + // Remove undefined values from the requestParams object + const queryParamsObject = pickBy(requestParams, identity); + const queryParamsString = new URLSearchParams(queryParamsObject).toString(); + + return `${this.EDIT_URL}?${queryParamsString}`; + }; +} diff --git a/app/client/src/ce/sagas/PageSagas.tsx b/app/client/src/ce/sagas/PageSagas.tsx index 18628faa1721..de16549df766 100644 --- a/app/client/src/ce/sagas/PageSagas.tsx +++ b/app/client/src/ce/sagas/PageSagas.tsx @@ -400,11 +400,13 @@ export function* fetchPublishedPageResourcesSaga( action: ReduxAction, ) { try { - const { basePageId, pageId } = action.payload; + const { basePageId, branch: branchName, pageId } = action.payload; - const params = { defaultPageId: basePageId }; const initConsolidatedApiResponse: ApiResponse = - yield ConsolidatedPageLoadApi.getConsolidatedPageLoadDataView(params); + yield ConsolidatedPageLoadApi.getConsolidatedPageLoadDataView({ + defaultPageId: basePageId, + branchName, + }); const isValidResponse: boolean = yield validateResponse( initConsolidatedApiResponse, diff --git a/app/client/src/ce/utils/serviceWorkerUtils.test.ts b/app/client/src/ce/utils/serviceWorkerUtils.test.ts index 5a71363c89e1..eb9b0b04c090 100644 --- a/app/client/src/ce/utils/serviceWorkerUtils.test.ts +++ b/app/client/src/ce/utils/serviceWorkerUtils.test.ts @@ -350,10 +350,9 @@ describe("serviceWorkerUtils", () => { expect(request).toBeInstanceOf(Request); expect(request?.url).toBe( - `https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${basePageId}&applicationId=${baseApplicationId}`, + `https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${basePageId}&applicationId=${baseApplicationId}&branchName=${params.branchName}`, ); expect(request?.method).toBe("GET"); - expect(request?.headers.get("Branchname")).toBe("main"); }); it("should create request for PUBLISHED mode with applicationId", () => { @@ -369,10 +368,9 @@ describe("serviceWorkerUtils", () => { expect(request).toBeInstanceOf(Request); expect(request?.url).toBe( - `https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=${basePageId}&applicationId=${baseApplicationId}`, + `https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=${basePageId}&applicationId=${baseApplicationId}&branchName=${params.branchName}`, ); expect(request?.method).toBe("GET"); - expect(request?.headers.get("Branchname")).toBe("main"); }); it("should create request for EDIT mode without applicationId", () => { @@ -387,10 +385,9 @@ describe("serviceWorkerUtils", () => { expect(request).toBeInstanceOf(Request); expect(request?.url).toBe( - `https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=page123`, + `https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${params.basePageId}&branchName=${params.branchName}`, ); expect(request?.method).toBe("GET"); - expect(request?.headers.get("Branchname")).toBe("main"); }); it("should create request for PUBLISHED mode without applicationId", () => { @@ -405,10 +402,9 @@ describe("serviceWorkerUtils", () => { expect(request).toBeInstanceOf(Request); expect(request?.url).toBe( - `https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=page123`, + `https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=${params.basePageId}&branchName=${params.branchName}`, ); expect(request?.method).toBe("GET"); - expect(request?.headers.get("Branchname")).toBe("main"); }); it("should return null for an unknown app mode", () => { @@ -438,10 +434,9 @@ describe("serviceWorkerUtils", () => { expect(consolidatedAPIRequest).toBeInstanceOf(Request); expect(consolidatedAPIRequest?.url).toBe( - `https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=page123`, + `https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${params.basePageId}&branchName=${params.branchName}`, ); expect(consolidatedAPIRequest?.method).toBe("GET"); - expect(consolidatedAPIRequest?.headers.get("Branchname")).toBe("main"); }); }); @@ -475,27 +470,16 @@ describe("serviceWorkerUtils", () => { describe("getRequestKey", () => { it("should return the correct request key", () => { - const request = new Request("https://app.appsmith.com", { - method: "GET", - }); - - request.headers.append("branchname", "main"); - const key = prefetchApiService.getRequestKey(request); - - expect(key).toBe("GET:https://app.appsmith.com/:branchname:main"); - }); - - it("should only append branchname header in request key", () => { - const request = new Request("https://app.appsmith.com", { - method: "GET", + const url = + "https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=page123&applicationId=app123&branchName=main"; + const method = "GET"; + const request = new Request(url, { + method, }); - request.headers.append("branchname", "main"); - request.headers.append("another-header-key", "another-header-value"); - request.headers.append("Content-Type", "application/json"); const key = prefetchApiService.getRequestKey(request); - expect(key).toBe("GET:https://app.appsmith.com/:branchname:main"); + expect(key).toBe(`${method}:${url}`); }); }); diff --git a/app/client/src/ce/utils/serviceWorkerUtils.ts b/app/client/src/ce/utils/serviceWorkerUtils.ts index 7d729e72826d..4c466bb0acf4 100644 --- a/app/client/src/ce/utils/serviceWorkerUtils.ts +++ b/app/client/src/ce/utils/serviceWorkerUtils.ts @@ -10,6 +10,7 @@ import { BUILDER_PATH_DEPRECATED, VIEWER_PATH_DEPRECATED, } from "../constants/routes/appRoutes"; +import { ConsolidatedApiUtils } from "api/services/ConsolidatedPageLoadApi/url"; interface TMatchResult { basePageId?: string; @@ -104,36 +105,36 @@ export const getConsolidatedApiPrefetchRequest = ( const { appMode, baseApplicationId, basePageId, branchName, origin } = applicationProps; - const headers = new Headers(); - const searchParams = new URLSearchParams(); - if (!basePageId) { return null; } - searchParams.append("defaultPageId", basePageId); - - if (baseApplicationId) { - searchParams.append("applicationId", baseApplicationId); - } - - // Add the branch name to the headers - if (branchName) { - headers.append("Branchname", branchName); - } - // If the URL matches the builder path if (appMode === APP_MODE.EDIT) { - const requestUrl = `${origin}/api/${"v1/consolidated-api/edit"}?${searchParams.toString()}`; - const request = new Request(requestUrl, { method: "GET", headers }); + const requestUrl = ConsolidatedApiUtils.getEditUrl({ + defaultPageId: basePageId, + applicationId: baseApplicationId, + branchName, + }); + + const request = new Request(`${origin}/api/${requestUrl}`, { + method: "GET", + }); return request; } // If the URL matches the viewer path if (appMode === APP_MODE.PUBLISHED) { - const requestUrl = `${origin}/api/v1/consolidated-api/view?${searchParams.toString()}`; - const request = new Request(requestUrl, { method: "GET", headers }); + const requestUri = ConsolidatedApiUtils.getViewUrl({ + defaultPageId: basePageId, + applicationId: baseApplicationId, + branchName, + }); + + const request = new Request(`${origin}/api/${requestUri}`, { + method: "GET", + }); return request; } @@ -166,22 +167,12 @@ export class PrefetchApiService { cacheMaxAge = 2 * 60 * 1000; // 2 minutes in milliseconds // Mutex to lock the fetch and cache operation prefetchFetchMutexMap = new Map(); - // Header keys used to create the unique request key - headerKeys = ["branchname"]; constructor() {} // Function to get the request key getRequestKey = (request: Request) => { - let requestKey = `${request.method}:${request.url}`; - - this.headerKeys.forEach((headerKey) => { - const headerValue = request.headers.get(headerKey); - - if (headerValue) { - requestKey += `:${headerKey}:${headerValue}`; - } - }); + const requestKey = `${request.method}:${request.url}`; return requestKey; }; diff --git a/app/client/src/pages/AppViewer/index.tsx b/app/client/src/pages/AppViewer/index.tsx index abe1ebbf3884..7121f3ede048 100644 --- a/app/client/src/pages/AppViewer/index.tsx +++ b/app/client/src/pages/AppViewer/index.tsx @@ -166,6 +166,7 @@ function AppViewer(props: Props) { fetchPublishedPageResources({ basePageId, pageId, + branch, }), ); } diff --git a/app/client/src/sagas/InitSagas.ts b/app/client/src/sagas/InitSagas.ts index fdfe0a3c8569..8ad5f7ff6cc9 100644 --- a/app/client/src/sagas/InitSagas.ts +++ b/app/client/src/sagas/InitSagas.ts @@ -1,4 +1,4 @@ -import { get, identity, pickBy } from "lodash"; +import get from "lodash/get"; import { all, call, @@ -221,6 +221,7 @@ function* executeActionDuringUserDetailsInitialisation( export function* getInitResponses({ applicationId, basePageId, + branch, mode, shouldInitialiseUserDetails, }: { @@ -228,16 +229,13 @@ export function* getInitResponses({ basePageId?: string; mode?: APP_MODE; shouldInitialiseUserDetails?: boolean; - // TODO: Fix this the next time the file is edited - // eslint-disable-next-line @typescript-eslint/no-explicit-any -}): any { - const params = pickBy( - { - applicationId, - defaultPageId: basePageId, - }, - identity, - ); + branch?: string; +}) { + const params = { + applicationId, + defaultPageId: basePageId, + branchName: branch, + }; let response: InitConsolidatedApi | undefined; try { diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java index 67eea1fc09c0..1f4770915131 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ConsolidatedAPIController.java @@ -14,7 +14,6 @@ import org.springframework.http.HttpStatus; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -49,9 +48,9 @@ public ConsolidatedAPIController( public Mono> getAllDataForFirstPageLoadForEditMode( @RequestParam(name = FieldName.APPLICATION_ID, required = false) String baseApplicationId, @RequestParam(name = "defaultPageId", required = false) String basePageId, - @RequestHeader(required = false, defaultValue = "branch") RefType refType, - @RequestHeader(required = false) String refName, - @RequestHeader(required = false) String branchName) { + @RequestParam(required = false, defaultValue = "branch") RefType refType, + @RequestParam(required = false) String refName, + @RequestParam(required = false) String branchName) { if (!StringUtils.hasLength(refName)) { refName = branchName; @@ -82,9 +81,9 @@ public Mono> getAllDataForFirstPageLoadF public Mono> getAllDataForFirstPageLoadForViewMode( @RequestParam(required = false) String applicationId, @RequestParam(required = false) String defaultPageId, - @RequestHeader(required = false, defaultValue = "branch") RefType refType, - @RequestHeader(required = false) String refName, - @RequestHeader(required = false) String branchName) { + @RequestParam(required = false, defaultValue = "branch") RefType refType, + @RequestParam(required = false) String refName, + @RequestParam(required = false) String branchName) { if (!StringUtils.hasLength(refName)) { refName = branchName;