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

HMR confuses global signals #512

Closed
rjvdw opened this issue Aug 12, 2023 · 3 comments
Closed

HMR confuses global signals #512

rjvdw opened this issue Aug 12, 2023 · 3 comments

Comments

@rjvdw
Copy link

rjvdw commented Aug 12, 2023

Describe the bug

When using preact with vite, a conflict arises when multiple contexts are defined using the same variable name. Even if these contexts are created in separate files, if they are assigned to the same variable name, they seem to override each other. This only happens when running npm run dev, not when running npm run build or npm run preview.

To Reproduce

I created a minimal example exhibiting this behavior: https://github.com/rjvdw/preact-context-bug. Foo.jsx, Bar.jsx and Baz.jsx all define their own context. Foo.jsx and Bar.jsx use const ctx = createContext, whereas Baz.jsx uses const ctxBaz = createContext.

Expected behavior

When running this app, you are presented with three buttons. I would expect each button to only increment the counter directly next to this button, but instead the counters for Foo.jsx and Bar.jsx are linked. I.e. when I increment Foo, Bar is also incremented and vice versa. The counter for Baz is working as intended.

@marvinhagemeister
Copy link
Member

marvinhagemeister commented Aug 12, 2023

I took a look into this and it seems to be caused by Prefresh our HMR implementation. When enabled two of the three signals share the exact same reference.

Screenshot 2023-08-12 at 17 54 28

When Prefresh is disabled it works as expected.

It can be disabled by changing the vite config to this:

export default defineConfig({
  plugins: [preact({ prefreshEnabled: false })],
});

I wonder if something isn't properly unsubscribed or something.

@marvinhagemeister marvinhagemeister changed the title Issue with conflicting contexts when they share a variable name HMR confuses global signals Aug 12, 2023
@marvinhagemeister marvinhagemeister transferred this issue from preactjs/preset-vite Aug 12, 2023
@rschristian
Copy link
Member

Might be related to #488?

In the linked issue there's an additional bug report of the fileHash being borked when the consumer uses the same default value and variableDeclaration for the createContext

@JoviDeCroock
Copy link
Member

This should be resolved by #524

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

No branches or pull requests

4 participants