Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React] Don't track signals read during SSR render #640

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

andrewiggins
Copy link
Member

@andrewiggins andrewiggins commented Jan 7, 2025

Currently during all renders we setup an effect to track which signals a component reads, so we can rerender that component when a signal it uses changes. When that component unmounts, React calls our unsubscribe function and we dispose of the created effects.

However, during an SSR render, React does not call our unsubscribe function. Internally we use useSyncExternalStore to hook up React to signals. Subscribing/unsubscribing to stores is treated as a passive effect (aka like a useEffect), which aren't invoked on SSR renders. So while we setup our effect to track signal usage during SSR, the corresponding unsubscribe method is never called at the end of SSR so the tracking objects are never cleaned up from the Signal.

In practice, each tracking object is ~100 bytes and so many applications may not notice this if they deploy frequently. You may notice GC times taking longer first before memory pressure. Regardless, this PR fixes it.

This PR fixes the memory leak by not creating any tracking effect if the window object is undefined.

Copy link

changeset-bot bot commented Jan 7, 2025

🦋 Changeset detected

Latest commit: 8108153

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@preact/signals-react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Jan 7, 2025

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit 8108153
🔍 Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/677ce7d22245630008300a2f
😎 Deploy Preview https://deploy-preview-640--preact-signals-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Jan 7, 2025

Size Change: +57 B (+0.07%)

Total Size: 83.1 kB

Filename Size Change
docs/dist/assets/client.********.js 46.2 kB +58 B (+0.13%)
docs/dist/nesting-********.js 1.13 kB -1 B (-0.09%)
ℹ️ View Unchanged
Filename Size
docs/dist/assets/bench.********.js 1.59 kB
docs/dist/assets/index.********.js 836 B
docs/dist/assets/jsxRuntime.module.********.js 284 B
docs/dist/assets/preact.module.********.js 4.03 kB
docs/dist/assets/signals-core.module.********.js 1.41 kB
docs/dist/assets/signals.module.********.js 2.16 kB
docs/dist/assets/style.********.js 21 B
docs/dist/assets/style.********.css 1.24 kB
docs/dist/basic-********.js 243 B
docs/dist/demos-********.js 3.44 kB
docs/dist/react-********.js 242 B
packages/core/dist/signals-core.js 1.45 kB
packages/core/dist/signals-core.mjs 1.47 kB
packages/preact/dist/signals.js 1.46 kB
packages/preact/dist/signals.mjs 1.43 kB
packages/react-transform/dist/signals-*********.js 4.93 kB
packages/react-transform/dist/signals-transform.mjs 4.16 kB
packages/react-transform/dist/signals-transform.umd.js 5.04 kB
packages/react/dist/signals.js 188 B
packages/react/dist/signals.mjs 150 B

compressed-size-action

@@ -312,7 +339,11 @@ export function _useSignalsImplementation(

const storeRef = useRef<EffectStore>();
if (storeRef.current == null) {
storeRef.current = createEffectStore(_usage);
if (typeof window === "undefined") {
Copy link
Member

Choose a reason for hiding this comment

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

The thing I am worried about here is react-native and others, shouldn't we detect this with i.e. layout-effect/effect firing?

Copy link
Member

Choose a reason for hiding this comment

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

I guess it's not really possible so I'd add a global so we have an escape hatch, like a function enableX which defaults to the typeof window check

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm true true...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea that may be a decent escape hatch, but Imma sit on this for a day or two more to see if some other solution presents itself.

What I'd really like is some hook or something that React would call as an effect in both SSR and client-side renders but I couldn't find one...

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@JoviDeCroock JoviDeCroock left a comment

Choose a reason for hiding this comment

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

Leaving my approval to unblock, pending the lint fix and maybe the global override for some environments

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