Skip to content

fix: Mark module as not containing side effectful code#141

Merged
rschristian merged 1 commit intomainfrom
fix/side-effects
Mar 30, 2026
Merged

fix: Mark module as not containing side effectful code#141
rschristian merged 1 commit intomainfrom
fix/side-effects

Conversation

@rschristian
Copy link
Copy Markdown
Member

@rschristian rschristian commented Mar 28, 2026

Reported here: https://bsky.app/profile/sdhfjksdf.bsky.social/post/3mi526vu74224

Can reproduce by creating a new preact project (pn create preact) with only prerendering enabled. Our option hook usage is side-effectful, but is fairly safe to shake out when the user isn't consuming lazy or <ErrorBoundary>.

For the example case above, in which the user is only using hydrate (and technically prerender, but that's split into a separate bundle entirely), the bundle size changes as such:

Before After
20.46 kB │ gzip: 8.57 kB 15.89 kB │ gzip: 6.81 kB

I spent a while playing with this and I couldn't get it to break with Vite. This seems to only shake at the sub-file level, which is precisely what we want, so that the _diff & _catchError augments (for example) are kept when using lazy or ErrorBoundary. Everything from that file will be shaken if none of its exports are used, as they should be.

@rschristian rschristian marked this pull request as draft March 28, 2026 20:42
@rschristian rschristian marked this pull request as ready for review March 28, 2026 21:06
"./prerender": "./src/prerender.js",
"./hydrate": "./src/hydrate.js"
},
"sideEffects": false,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure this is safe everywhere, we might need to do a if (!initialized) registerOptionsHooks() in the relevant components

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How so?

The only issue I can see is if the user is using the <Router> as an implicit suspense boundary without ever using lazy() (or ErrorBoundary) -- this is a very unlikely scenario, given suspense's low usage, nor do I think it's one we should support by injecting more code than actually asked for or needed.

As for normal usage, every modern bundler will see the side-effectful usage within individual modules and keep the hooks around if the module as a whole is ever loaded, i.e., if the user ever imports lazy or ErrorBoundary. I don't know how non-modern bundlers act here but I don't think we need to be preemptively concerned.

I don't foresee anything else being problematic here, but perhaps I'm missing something?

@rschristian rschristian merged commit daa9bc1 into main Mar 30, 2026
1 check passed
@rschristian rschristian deleted the fix/side-effects branch March 30, 2026 17:34
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