From 387fd31c04250d3b198d0095f8104ef16817f63e Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Wed, 31 Mar 2021 11:56:48 +0200 Subject: [PATCH] fix(ssr): correctly pass scopedResults.results (algolia/vue-instantsearch#943) * fix(ssr): correctly pass scopedResults.results We retrieve the scoped results from the parent index in the initial render on server & client, however since the index widget doesn't have its results from a search query yet, `getResults` (used inside getScopedResults) will be `null` (derivedHelper && derivedHelper.lastResults). The solution is to augment the retrieved scoped results with the actual search results from __initialResults, like done for `results`. fixes algolia/vue-instantsearch#940 * compat --- .../__tests__/createServerRootMixin.test.js | 24 +++++++++---------- .../src/util/createServerRootMixin.js | 6 ++++- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js b/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js index afec17dc74..6b3da8dff1 100644 --- a/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js +++ b/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js @@ -718,35 +718,35 @@ Array [ expect(renderArgs).toMatchInlineSnapshot( { - helper: expect.any(Object), - results: expect.any(Object), + helper: expect.anything(), + results: expect.anything(), scopedResults: expect.arrayContaining([ expect.objectContaining({ - helper: expect.any(Object), + helper: expect.anything(), indexId: expect.any(String), - results: expect.any(Object), + results: expect.anything(), }), ]), - state: expect.any(Object), - instantSearchInstance: expect.any(Object), + state: expect.anything(), + instantSearchInstance: expect.anything(), }, ` Object { "createURL": [Function], - "helper": Any, - "instantSearchInstance": Any, - "results": Any, + "helper": Anything, + "instantSearchInstance": Anything, + "results": Anything, "scopedResults": ArrayContaining [ ObjectContaining { - "helper": Any, + "helper": Anything, "indexId": Any, - "results": Any, + "results": Anything, }, ], "searchMetadata": Object { "isSearchStalled": false, }, - "state": Any, + "state": Anything, "templatesConfig": Object {}, } ` diff --git a/packages/vue-instantsearch/src/util/createServerRootMixin.js b/packages/vue-instantsearch/src/util/createServerRootMixin.js index 22464f9e8e..ebbee69155 100644 --- a/packages/vue-instantsearch/src/util/createServerRootMixin.js +++ b/packages/vue-instantsearch/src/util/createServerRootMixin.js @@ -187,7 +187,11 @@ function augmentInstantSearch( widget.render({ helper: localHelper, results, - scopedResults: parent.getScopedResults(), + scopedResults: parent.getScopedResults().map(result => + Object.assign(result, { + results: search.__initialSearchResults[result.indexId], + }) + ), state, templatesConfig: {}, createURL: parent.createURL,