Skip to content

fix: destroy Packery instance on unmount to stop memory leak in SponsorBubbles#233

Merged
VoxelFS merged 1 commit into
mainfrom
memory-leak-fix
May 6, 2026
Merged

fix: destroy Packery instance on unmount to stop memory leak in SponsorBubbles#233
VoxelFS merged 1 commit into
mainfrom
memory-leak-fix

Conversation

@VoxelFS

@VoxelFS VoxelFS commented May 6, 2026

Copy link
Copy Markdown
Contributor

from claude:

Summary

Fixes a memory leak on the home page caused by SponsorBubbles creating a new
Packery layout instance on every mount and on every baseSize change without
ever destroying the previous one. Each navigation back to / left behind a
full Packery instance plus all its DOM references, which Chrome's heap profiler
showed as a steadily growing pile of detached nodes and PackeryItem objects.

Root cause

In src/app/(frontend)/(home)/_components/SponsorBubbles.tsx, the Packery
effect was calling new Packery(...) and discarding the return value:

new Packery(containerRef.current, { ... });

Packery attaches listeners to the container and holds references to every
matched grid item. Without a handle to the instance, there was no way to call
.destroy(), so each remount stacked another live instance on top of the
previous one.

A secondary issue: the scroll-listener effect listed hoveredSponsor in its
dependency array, so the listener was being torn down and re-attached on every
hover change.

Changes

  • Store the Packery instance in a useRef and call .destroy() in the effect
    cleanup (runs on unmount and before re-init when baseSize changes).
  • Add a cancelled flag so a late-resolving dynamic import('packery') does
    not construct an orphaned instance after unmount.
  • Switch the scroll handler to a functional setHoveredSponsor update and drop
    hoveredSponsor from the effect deps so the listener attaches once.

@VoxelFS
VoxelFS requested a review from koutaroyumiba May 6, 2026 09:14

@koutaroyumiba koutaroyumiba left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm - what a cooked bug

@VoxelFS
VoxelFS merged commit 69a797d into main May 6, 2026
2 checks passed
@VoxelFS
VoxelFS deleted the memory-leak-fix branch May 6, 2026 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants