From f6fa5d6e8d74a472906d642f0f36bed82bd87b6f Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 12 Feb 2024 11:47:40 -0700 Subject: [PATCH] fix(controller/search): modifying logic for infinite backfill to optimize cache --- packages/snap-client/src/Client/apis/Legacy.ts | 1 + .../src/Search/SearchController.test.ts | 5 +++++ .../src/Search/SearchController.ts | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/snap-client/src/Client/apis/Legacy.ts b/packages/snap-client/src/Client/apis/Legacy.ts index d6cb12f1b..f052d6f0b 100644 --- a/packages/snap-client/src/Client/apis/Legacy.ts +++ b/packages/snap-client/src/Client/apis/Legacy.ts @@ -11,6 +11,7 @@ export class LegacyAPI extends API { //remove pageLoadId from cache key query params const cacheParameters = { ...queryParameters }; delete cacheParameters.pageLoadId; + delete cacheParameters.domain; const legacyResponse = await this.request( { diff --git a/packages/snap-controller/src/Search/SearchController.test.ts b/packages/snap-controller/src/Search/SearchController.test.ts index 773cee04f..bdefc751d 100644 --- a/packages/snap-controller/src/Search/SearchController.test.ts +++ b/packages/snap-controller/src/Search/SearchController.test.ts @@ -416,6 +416,11 @@ describe('Search Controller', () => { const { pageSize } = controller.store.pagination; expect(searchfn).toHaveBeenCalledTimes(page); + + expect(searchfn).toHaveBeenNthCalledWith(1, expect.objectContaining({ pagination: {} })); + expect(searchfn).toHaveBeenNthCalledWith(2, expect.objectContaining({ pagination: { page: 2 }, search: { redirectResponse: 'full' } })); + expect(searchfn).toHaveBeenNthCalledWith(3, expect.objectContaining({ pagination: { page: 3 }, search: { redirectResponse: 'full' } })); + expect(controller.store.results.length).toBe(pageSize * page); searchfn.mockClear(); diff --git a/packages/snap-controller/src/Search/SearchController.ts b/packages/snap-controller/src/Search/SearchController.ts index 8655d89ba..f04f28a32 100644 --- a/packages/snap-controller/src/Search/SearchController.ts +++ b/packages/snap-controller/src/Search/SearchController.ts @@ -272,6 +272,7 @@ export class SearchController extends AbstractController { const params: SearchRequestModel = deepmerge({ ...getSearchParams(this.urlManager.state) }, this.config.globals || {}); // redirect setting + // DUPLICATED LOGIC can be found in infinite backfill (change both if updating) if (!this.config.settings?.redirects?.merchandising || this.store.loaded) { params.search = params.search || {}; params.search.redirectResponse = 'full' as SearchRequestModelSearchRedirectResponseEnum; @@ -376,7 +377,21 @@ export class SearchController extends AbstractController { const backfillRequests = Array(params.pagination.page) .fill('backfill') .map((v, i) => { - const backfillParams = deepmerge({ ...params }, { pagination: { page: i + 1 } }); + const backfillParams: SearchRequestModel = deepmerge( + { ...params }, + { pagination: { page: i + 1 }, search: { redirectResponse: 'full' } } + ); + // don't include page parameter if on page 1 + if (i + 1 == 1) { + delete backfillParams?.pagination?.page; + + if (this.config.settings?.redirects?.merchandising) { + // redirect setting + // DUPLICATED LOGIC can be found in params getter + delete backfillParams?.search?.redirectResponse; + } + } + return this.client.search(backfillParams); });