From 54a462cddc4c3cf10d8e00100eb0fc4e0745bef9 Mon Sep 17 00:00:00 2001 From: snehankekre Date: Mon, 6 Feb 2023 17:03:16 +0530 Subject: [PATCH] Fix memory leak in helful.js utility --- components/utilities/helpful.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/utilities/helpful.js b/components/utilities/helpful.js index 16094529..4641e8ae 100644 --- a/components/utilities/helpful.js +++ b/components/utilities/helpful.js @@ -83,9 +83,14 @@ const Helpful = ({ slug, sourcefile }) => { setIsHelpful(true); }; + // Perform the route change cleanup function for the Helpful component inside a useEffect call, + // instead of using router.events.on("routeChangeComplete", handleRouteChange), because that + // adds new events progressively as you keep browsing the website, thus eventually leading to a memory leak. useEffect(() => { - router.events.on("routeChangeComplete", handleRouteChange); - }); + return () => { + handleRouteChange(); + }; + }, [sourcefile]); let joinedSlug = "/"; if (slug) {