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

[@vitejs/plugin-react] React 18's bootstrapModules option incompatible with Vite's React plugin #14

Closed
7 tasks done
jplhomer opened this issue Feb 4, 2022 · 6 comments

Comments

@jplhomer
Copy link

jplhomer commented Feb 4, 2022

Describe the bug

React 18 introduces a new renderToPipeableStream pattern, which includes options for adding client entrypoints via bootstrapScripts and bootstrapModules:

renderToPipeableStream(
  <App />,
  {
    bootstrapModules: ["src/main.jsx"],
  }
).pipe(response);

Script tags generated from these options include the async property. This allows React to progressively hydrate the document as it streams in.

Unfortunately, this is incompatible with the React Refresh header added in @vitejs/plugin-react. I assume this is because the header code throws an Error if it cannot detect the preamble (which is still loaded eventually, just not asynchronously):

Html.jsx:6 Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong. See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201
    at Html.jsx:6:11
(anonymous) @ Html.jsx:6
client.ts:52 

It's odd that I cannot reproduce this behavior by simply adding async to the script tag in the React starter template: https://stackblitz.com/edit/vitejs-vite-fpvd7t?file=index.html

But I can when it's included via bootstrapModules: jplhomer/vite-streaming-ssr-demo#3

Perhaps this means that the way React is injecting the script makes it execute earlier than it normally does? I'm kind of stumped.

Not sure what the best approach to solving this is! I think it's important that entrypoint scripts can be async to support streaming SSR in React 18.

Should we to update the injected header to more gracefully wait for the RefreshRuntime to be available?

Reproduction

jplhomer/vite-streaming-ssr-demo#3

System Info

System:
    OS: macOS 12.2
    CPU: (8) arm64 Apple M1 Pro
    Memory: 133.94 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.1/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-react: ^1.1.4 => 1.1.4 
    vite: ^2.7.13 => 2.7.13

Used Package Manager

yarn

Logs

No response

Validations

@cyco130
Copy link

cyco130 commented Mar 12, 2022

It seems to work when I add the async attribute to the preamble script.

@Trackhe
Copy link

Trackhe commented Jun 2, 2022

Do you know what i missed? Because i cant get SSR working with the Production build: https://github.com/Trackhe/Vite-React-SSR-HMR
vitejs/vite#8449

@b3nten
Copy link

b3nten commented Aug 7, 2022

It seems to work when I add the async attribute to the preamble script.

Doesn't seem to be working for me unfortunately. The workaround seems to be loading the script as part of the stream.

@patak-dev patak-dev transferred this issue from vitejs/vite Dec 3, 2022
@letientai299
Copy link

letientai299 commented Jan 7, 2023

I hit this same problem and managed to find a work around. I put together a demo below. Perhaps someone with more knowledge on Vite could derives a proper solution to make React SSR more straight forward with Vite.

https://github.com/letientai299/vite-react-ssr-typescript

@ArnaudBarre
Copy link
Member

ArnaudBarre commented Jan 22, 2023

Hi everyone!

I currently investigate open issues to work on a new API for the next major. I understand that there is an issue with renderToPipeableStream and script loading order, but I don't see any quick reproduction to look at it (only a closed PR on repo)

Can someone provide a small reproduction of the issue? Thanks

@github-actions
Copy link

Hello @jplhomer. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@jplhomer jplhomer closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Feb 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants