algolia / instantsearch

⚡️ Libraries for building performant and instant search and recommend experiences with Algolia. Compatible with JavaScript, TypeScript, React and Vue.
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/
MIT License
3.73k stars 526 forks source link

fix(next-app-router): mitigate memory leak #6372

Closed aymeric-giraudet closed 2 months ago

aymeric-giraudet commented 2 months ago

Summary

Got reports of memory leaks with the Next.js App Router solution. It seems partly related to ServerInsertedHTML or maybe just the fact that renders are streamed.

Result

Prior to the change, on an index with stringified results weighing ~50kb, for 500 concurrent requests we had a peak of 1GB which reduced to 600mb when idle. By moving the initialisation of createInsertHTML so that it does not capture the scope of the whole component, it peaks at 600mb then reduces to 160mb when idle.

There are definitely more ways this could be improved as I see stringified results are kept in memory way after, so does certain React nodes with results in them. Should try to see with the Next.js team perhaps.

codesandbox-ci[bot] commented 2 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6b4141a2440533ca59607a7e68e390f27755508d:

Sandbox Source
example-instantsearch-getting-started Configuration
example-react-instantsearch-getting-started Configuration
example-react-instantsearch-next-app-dir-example Configuration
example-react-instantsearch-next-routing-example Configuration
example-vue-instantsearch-getting-started Configuration
pkutsenko commented 1 month ago

Hi @aymeric-giraudet It seems like next.js with its addInsertedHtml function causes a memory leak issue. If I comment out insertHTML call the memory leak is gone that of course breaks SSR. Have you considered not using ServerInsertedHTML but return Githubissues.

  • Githubissues is a development platform for aggregating issues.