Skip to content

fix: Memory leak — resize event listener never removed in SidebarLayout #2588

Closed

Description

@Sarthak-Pandey

Describe the Bug

In components/Sidebar.tsx, the SidebarLayout component attaches a resize event listener to window inside a useEffect using an anonymous function, but does not provide a cleanup function to remove it on unmount.

Consequently, each time a user navigates between pages that mount SidebarLayout, a new listener is added to window while previous listeners remain in memory. Over prolonged navigation sessions, these stacked listeners accumulate, causing unnecessary re-renders and memory leakage when resizing the viewport.

Additionally, the dependency array [typeof window !== 'undefined'] evaluates to [true] on the client side, acting as a misleading no-op that should be [].


Affected Code

File: components/Sidebar.tsx (lines 207–215)

useEffect(() => {
  if (window) {
    window.addEventListener('resize', () => {
      if (window.innerWidth > 1024) {
        setOpen(false);
      }
    });
  }
}, [typeof window !== 'undefined']);

Activity

  1. added
    duplicateThis issue or pull request already exists
    invalidThis doesn't seem right
    Status: TriageThis is the initial status for an issue that requires triage.
    on Oct 24, 2026
  2. Sarthak-Pandey commented on Oct 24, 2026

    @Sarthak-Pandey
    ContributorAuthor

    Hi @Relequestual
    I'd like to work on this issue. I've already identified the root cause and have a fix ready.

  3. vtushar06 commented on Oct 24, 2026

    @vtushar06
    Collaborator

    can you restructure issues description for better clarification. @Sarthak-Pandey

  4. Sarthak-Pandey commented on Oct 24, 2026

    @Sarthak-Pandey
    ContributorAuthor

    Hi @vtushar06, I've updated and restructured the issue description with clear sections, reproduction steps (using DevTools event listener inspection), root cause details, and a proposed code diff matching the codebase's existing conventions. Please take a look!

  5. Sarthak-Pandey commented on Oct 24, 2026

    @Sarthak-Pandey
    ContributorAuthor

    Proposed Solution

    To resolve the memory leak and clean up the effect implementation in components/Sidebar.tsx:

    1. Extract Named Handler & Add Cleanup: Define handleResize as a named function reference and return a cleanup teardown callback invoking window.removeEventListener('resize', handleResize) when the component unmounts.
    2. Clean Dependency Array: Replace [typeof window !== 'undefined'] with []. In the browser, typeof window !== 'undefined' evaluates to a constant [true], which is misleading.
    3. Remove Redundant Guard: Drop if (window) as useEffect exclusively runs client-side post-mount where window is guaranteed.

    This aligns with the existing listener cleanup pattern in components/Layout.tsx.

    Diff:

    -  useEffect(() => {
    -    if (window) {
    -      window.addEventListener('resize', () => {
    -        if (window.innerWidth > 1024) {
    -          setOpen(false);
    -        }
    -      });
    -    }
    -  }, [typeof window !== 'undefined']);
    +  useEffect(() => {
    +    const handleResize = () => {
    +      if (window.innerWidth > 1024) {
    +        setOpen(false);
    +      }
    +    };
    +
    +    window.addEventListener('resize', handleResize);
    +    return () => {
    +      window.removeEventListener('resize', handleResize);
    +    };
    +  }, []);
  6. vtushar06 commented on Oct 25, 2026

    @vtushar06
    Collaborator

    Changes looks valid, Proceed with your PR @Sarthak-Pandey

  7. Sarthak-Pandey commented on Oct 25, 2026

    @Sarthak-Pandey
    ContributorAuthor

    Hi @vtushar06,

    I have opened a pull request resolving this issue: #2595

    The PR extracts the resize listener to a named handler, adds proper cleanup with removeEventListener on component unmount, cleans up the dependency array, and aligns with the existing pattern in components/Layout.tsx.

    Whenever you have a moment, please take a look. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Status: TriageThis is the initial status for an issue that requires triage.duplicateThis issue or pull request already existsinvalidThis doesn't seem right

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions