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

feat: implement upstream version of server components #498

Merged
merged 6 commits into from
Jan 21, 2022
Merged

Conversation

jplhomer
Copy link
Contributor

@jplhomer jplhomer commented Jan 21, 2022

BREAKING CHANGES:

  • Context is not supported in server components until Server Context lands upstream in React. A workaround is to use context in client components only
  • React Router has been removed from the server. This means useParams() is no longer available in server components. Instead, use the params prop provided to each page server component.
  • shopifyConfig must be provided to renderHydrogen in both entry-client.jsx and entry-server.jsx

Description

In mid-2021, we created an implementation of React Server Components that mimicked the official experimental version. However, this implementation was inefficient in that it couldn't be streamed and required much more work on the server.

This PR adopts the official experimental version under the hood in Hydrogen and removes the homemade implementation.

Fixes #252
Fixes #251
Fixes #250
Fixes #101

A couple notes:

  • We're seeing some issues with react-helmet-async during the RSC > SSR process. Need to investigate after we merge.
  • This relies on node/stream which is only available in > Node v16. We should follow up with a PR to replace our dependency on native streams with a polyfill so we can keep using Node v14 in StackBlitz
  • We're not sure exactly how well this plays with Cloudflare and Oxygen. We should investigate and make changes as necessary after we merge.

Before submitting the PR, please make sure you do the following:

  • Add your change under the Unreleased heading in the package's CHANGELOG.md
  • Read the Contributing Guidelines
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123)
  • Update docs in this repository for your change, if needed

frandiox and others added 5 commits January 19, 2022 10:19
* wip: Create custom flight renderer with local version of react-server

* wip: mock client manifest and add module reference to client components

* wip: hack RSC resolution

* wip: wrap component in Proxy to access original properties

* refactor: rename RSC server files

* wip: add official RSC hydrator

* fix: Rename response.readRoot and remove explicit hydration

* wip: remove RR and Helmet providers from the server

* wip: Add test app

* wip: remove hydration providers

* wip: add renderToReadableStream for RSC

* refactor: cleanup custom RSC code

* refactor: move and rename files

* wip: fix test app

* refactor: cleanup custom RSC code

* refactor: do not pass named boolean in RSC

* refactor: simplify ClientMarker

* test: fix ClientMarker tests

* fix: delay throwing error when client component is missing

* refactor: simplify import globs code

* feat: provide request object to rendering tree

* wip: example of useServerRequest

* feat: inline RSC response in the SSR HTML response

* fix: add default value to the SSR provider

* feat: stream rsc in script tags

* feat: update the starter template to work

* fix: lint errors

* refactor: move code around and cleanup

* chore: upgrade Vite to 2.7.0

* chore: add react-server-dom-vite as vendor

* feat: replace local react-server and react-client with react-server-dom-vite vendor

* fix: react-server-dom-vite allow exporting hooks from client components temporarily

* fix: move the server logging locations

* feat: Implement ReadableStream branch in SSR and RSC

* test: fix playground apps

* fix: avoid importing undefined exports depending on the running environment

* feat: Buffer RSC response if it cannot be streamed

* fix: tests

* fix: e2e test

* fix: linting

* fix: rename test helper

* fix: update hydrogen template files

* feat: remove react-ssr-prepass. RSC already makes ReadableStream required

* refactor: simplify hydration calls

* feat: remove old dependencies

* feat: enable tree shaking in worker build

* fix: Release stream lock before piping

* chore: fix formatting

* refactor: move code to entry-server to be consistent

* refactor: remove HydrationWriter in favor of Node native PassThrough

* fix: move constant to a separate file to avoid importing app logic in GraphiQL

* refactor: remove old code

* fix: maybe fix tests for windows

* fix: stream import in workers

* fix: flush RSC right after writing head

* feat: replace RenderCacheProvider with new ServerRequestProvider cache

* refactor: cleanup

* fix: suspense breaking hydration

* fix: normalize RSC chunks

* refactor: simplify customBody check

* feat: minor tree-shaking improvements

* fix: enable browser hydration

* fix: replace TransformStream with ReadableStream to support Firefox

* refactor: cleanup and rename variables

* fix: normalizePath

* fix: better regex

* fix: request.context property is reserved in CFW

* perf: Do not clone request to avoid extra memory and warnings in CFW

* chore: add TODO to remove weird Suspense boundary

* fix: Add back ShopifyProvider; call setShopConfig as part of renderHydrogen

* chore: remove RSCTest demo code

* fix: useMemo is allowed in RSC

* fix: Dot not create Response with streams to support CFW

Co-authored-by: Bret Little <[email protected]>
Co-authored-by: M. Bagher Abiat <[email protected]>
Co-authored-by: Josh Larson <[email protected]>
* wip: Create custom flight renderer with local version of react-server

* wip: mock client manifest and add module reference to client components

* wip: hack RSC resolution

* wip: wrap component in Proxy to access original properties

* refactor: rename RSC server files

* wip: add official RSC hydrator

* fix: Rename response.readRoot and remove explicit hydration

* wip: remove RR and Helmet providers from the server

* wip: Add test app

* wip: remove hydration providers

* wip: add renderToReadableStream for RSC

* refactor: cleanup custom RSC code

* refactor: move and rename files

* wip: fix test app

* refactor: cleanup custom RSC code

* refactor: do not pass named boolean in RSC

* refactor: simplify ClientMarker

* test: fix ClientMarker tests

* fix: delay throwing error when client component is missing

* refactor: simplify import globs code

* feat: provide request object to rendering tree

* wip: example of useServerRequest

* feat: inline RSC response in the SSR HTML response

* fix: add default value to the SSR provider

* feat: stream rsc in script tags

* feat: update the starter template to work

* fix: lint errors

* refactor: move code around and cleanup

* chore: upgrade Vite to 2.7.0

* chore: add react-server-dom-vite as vendor

* feat: replace local react-server and react-client with react-server-dom-vite vendor

* fix: react-server-dom-vite allow exporting hooks from client components temporarily

* fix: move the server logging locations

* feat: Implement ReadableStream branch in SSR and RSC

* test: fix playground apps

* fix: avoid importing undefined exports depending on the running environment

* feat: Buffer RSC response if it cannot be streamed

* fix: tests

* fix: e2e test

* fix: linting

* fix: rename test helper

* fix: update hydrogen template files

* feat: remove react-ssr-prepass. RSC already makes ReadableStream required

* refactor: simplify hydration calls

* feat: remove old dependencies

* feat: enable tree shaking in worker build

* fix: Release stream lock before piping

* chore: fix formatting

* refactor: move code to entry-server to be consistent

* refactor: remove HydrationWriter in favor of Node native PassThrough

* fix: move constant to a separate file to avoid importing app logic in GraphiQL

* refactor: remove old code

* fix: maybe fix tests for windows

* fix: stream import in workers

* fix: flush RSC right after writing head

* feat: replace RenderCacheProvider with new ServerRequestProvider cache

* refactor: cleanup

* fix: suspense breaking hydration

* fix: normalize RSC chunks

* refactor: simplify customBody check

* feat: minor tree-shaking improvements

* fix: enable browser hydration

* fix: replace TransformStream with ReadableStream to support Firefox

* refactor: cleanup and rename variables

* feat: Make ReadableStream globally available

* wip: consume RSC response in server

* wip: do not close stream connection

* wip: fix reentrant error with ReadableStreams in RSC

* fix: import Node RSC logic dynamically

* fix: rename Cache.client.js to avoid bundling it in workers build

* fix: add temporary monkey patch for React Fizz in workers build

* feat: Combine SSR and RSC responses in Node

* fix: write RSC before ending response when not streaming

* feat: Combine SSR and RSC responses in Worker

* feat: Use ReadableStream in CFW and stream if supported

* fix: add another temporary monkey patch for React Fizz/Flight in workers build

* fix: close writable on redirects

* fix: Fix hydration during dev by using more Suspense boundaries

* fix: add back response.socket listener

* feat: support script nonce and shorten access to __flight

* feat: avoid writing fractional chunks in SSR

* fix: initialize flight container in buffered rendering

* fix: ensure flight container is added in Node

* refactor: `readable` to `ssrReadable` for clarity

Co-authored-by: Bret Little <[email protected]>
Co-authored-by: Josh Larson <[email protected]>
@jplhomer jplhomer requested a review from a team January 21, 2022 16:24
@jplhomer jplhomer merged commit 525ec53 into main Jan 21, 2022
@jplhomer jplhomer deleted the experimental branch January 21, 2022 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants