Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(core): fix maximum call stack size exceeded (#2926)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored May 18, 2020
1 parent 6516acf commit 7e883df
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
11 changes: 6 additions & 5 deletions examples/server-side-rendering/src/createApp.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import algoliasearch from 'algoliasearch/lite';

import App from './App';

const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);

export const createApp = () => {
const indexName = 'instant_search';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);

const props = {
indexName,
searchClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,52 @@ describe('createInstantSearchManager', () => {

expect(Object.keys(searchClient.cache)).toHaveLength(0);
});

it('when using algoliasearch@v4, it overrides search only once', () => {
const searchClient = algoliasearch('appId', 'apiKey', {
_cache: true,
});

// Skip this test with Algoliasearch API Client < v4, as
// search does not need to be overridden.
if (!searchClient.transporter) {
return;
}

const resultsState = {
rawResults: [
{
index: 'indexName',
query: 'query',
},
],
state: {
index: 'indexName',
query: 'query',
},
};

const originalSearch = algoliasearch.search;

createInstantSearchManager({
indexName: 'index',
searchClient,
resultsState,
});

expect(searchClient.search).not.toBe(originalSearch);

const alreadyOverridden = jest.fn();
searchClient.search = alreadyOverridden;

createInstantSearchManager({
indexName: 'index',
searchClient,
resultsState,
});

expect(searchClient.search).toBe(alreadyOverridden);
});
});

describe('results hydratation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,9 @@ export default function createInstantSearchManager({
// Disable cache hydration on:
// - Algoliasearch API Client < v4 with cache disabled
// - Third party clients (detected by the `addAlgoliaAgent` function missing)

if (
!client.transporter &&
(!client.transporter || client._cacheHydrated) &&
(!client._useCache || typeof client.addAlgoliaAgent !== 'function')
) {
return;
Expand All @@ -341,7 +342,9 @@ export default function createInstantSearchManager({
// for us to compute the key the same way as `algoliasearch-client` we need
// to populate it on a custom key and override the `search` method to
// search on it first.
if (client.transporter) {
if (client.transporter && !client._cacheHydrated) {
client._cacheHydrated = true;

const baseMethod = client.search;
client.search = (requests, ...methodArgs) => {
const requestsWithSerializedParams = requests.map(request => ({
Expand Down

0 comments on commit 7e883df

Please sign in to comment.