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

Fix React adapter in SSR and when rerendering #355

Merged
merged 26 commits into from
Apr 24, 2023
Merged

Conversation

andrewiggins
Copy link
Member

Updates the react adapter to handle SSR and components that call setState while rendering.

I've also added a suite of mocha test that run in NodeJS and use react-dom/server to validate our adapter works in that environment.

Note:

  • The @preact/signals-react package can not be lazily imported. It must be imported before you call ReactDOM.render, ReactDOM.hydrate, renderToStaticMarkup, etc.
  • Your bundler must allow overriding other modules' exports. We use this capability to wrap React's createElement and related methods to handle passing signals as attributes to DOM elements.

Fixes #350

@changeset-bot
Copy link

changeset-bot bot commented Apr 21, 2023

🦋 Changeset detected

Latest commit: eba0b93

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

@netlify
Copy link

netlify bot commented Apr 21, 2023

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit eba0b93
🔍 Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/6442dbbdd4c38f0008026670
😎 Deploy Preview https://deploy-preview-355--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 settings.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 21, 2023

Size Change: +378 B (+1%)

Total Size: 68.9 kB

Filename Size Change
docs/dist/assets/client.********.js 46.5 kB +114 B (0%)
docs/dist/nesting-********.js 1.13 kB -1 B (0%)
docs/dist/react-********.js 237 B -2 B (-1%)
packages/react/dist/signals.js 1.21 kB +132 B (+12%) ⚠️
packages/react/dist/signals.mjs 1.15 kB +135 B (+13%) ⚠️
ℹ️ View Unchanged
Filename Size
docs/dist/assets/index.********.js 835 B
docs/dist/assets/jsxRuntime.module.********.js 282 B
docs/dist/assets/preact.module.********.js 4 kB
docs/dist/assets/signals-core.module.********.js 1.42 kB
docs/dist/assets/signals.module.********.js 1.97 kB
docs/dist/assets/style.********.js 21 B
docs/dist/assets/style.********.css 1.21 kB
docs/dist/basic-********.js 244 B
docs/dist/demos-********.js 3.35 kB
packages/core/dist/signals-core.js 1.48 kB
packages/core/dist/signals-core.mjs 1.5 kB
packages/preact/dist/signals.js 1.2 kB
packages/preact/dist/signals.mjs 1.15 kB

compressed-size-action

Comment on lines +43 to +50
coverage && [
"istanbul",
{
// TODO: Currently NodeJS tests always run against dist files. Should we
// change this?
// include: minify ? "**/dist/**/*.js" : "**/src/**/*.{js,jsx,ts,tsx}",
},
],
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: coverage isn't reported from this test run and I got tired and didn't set up running tests against source. If someone is feeling motivated and wants to do so, please do!

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.

Damn, this was some research work I reckon :o

Comment on lines +190 to +200
/*
Below is a state machine definition for transitions between the various
dispatchers in React's prod build. (It does not include dev time warning
dispatchers which are just always ignored).

ENTER and EXIT suffixes indicates whether this ReactCurrentDispatcher transition
signals we are entering or exiting a component render, or if it doesn't signal a
change in the component rendering lifecyle (NOOP).

```js
// Paste this into https://stately.ai/viz to visualize the state machine.
Copy link
Member

Choose a reason for hiding this comment

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

This is pretty darn neat

Copy link
Member

@rschristian rschristian left a comment

Choose a reason for hiding this comment

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

One of those rare PRs where the comments are so good that I came away feeling like I learned a thing or two.

Those things might be semi-cursed knowledge of React internals, but hey, knowledge is knowledge! 😅

@andrewiggins
Copy link
Member Author

Haha @rschristian definitely some cursed knowledge for sure 🧙🪄🔮

@andrewiggins andrewiggins merged commit 6b6af05 into main Apr 24, 2023
@andrewiggins andrewiggins deleted the react-adapter-fixes branch April 24, 2023 22:28
@github-actions github-actions bot mentioned this pull request Apr 24, 2023
@XantreDev
Copy link
Contributor

There are really hard job with researching weird react internals. Really appreciate.
I am for now fan of @andrewiggins btw

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.

Bug - v1.3.x Throws Errors when Used in React
5 participants