Unfortunately using Meteor.callAsync().then(setData) currently does not work for me (with Meteor version 2.14) as renderToString does not wait for any Promises to resolve. As for now, I am writing a custom hook for data retrieval on the server side and making a loop to keep rendering until all promises are fulfilled. This is the same as @macrozone mentioned above (solution b).
I’m not necessarily up-to-date with the latest React SSR state, but my take is that:
You don’t need React Server Components.
Using Suspense should work just fine, and there’s nothing Meteor-specific here.
Any Promise can suspend a component, but the management of throwing and caching them around is kind of tricky. We solved it for, e.g., useSubscribe, just as @grubba said, but Meteor.call would need a separate logic.
In the meantime, you could something like this package (I didn’t use it myself; just searched for “react suspense use promise”).
@grubba tried using Suspense in Meteor SSR and it worked as expected, i.e., sent a “shell” immediately and streamed data when it was ready.
I remember there was also an option to wait for everything before sending anything (i.e., no streaming).
This package @pbya suggested is kind of like Suspense, but globally, i.e., there’s no intermediate shell being sent to the client. (There’s also no error handling whatsoever.)
Suspense will send a ready HTML (i.e., with the data) if it’s loaded “fast enough” (I don’t think it’s not definied directly) or an empty shell (i.e., loading screens).
Here is the relevant function in React 18: renderToPipeableStream
There are two (2) important options:
Option 1:
optionalonShellReady: A callback that fires right after the initial shell has been rendered. You can set the status code and call pipe here to start streaming. React will stream the additional content after the shell along with the inline <script> tags that place that replace the HTML loading fallbacks with the content.
Option 2:
optionalonAllReady: A callback that fires when all rendering is complete, including both the shell and all additional content. You can use this instead of onShellReadyfor crawlers and static generation. If you start streaming here, you won’t get any progressive loading. The stream will contain the final HTML.
We still have not moved to React 18, with Meteor 3 being a blocker for us, so we have not tested these yet.
Looking at this, it looks like this requires support from webapp e.g. allowing pipe() function and response object to be accessible
While studying renderToPipeableStream(), it becomes clear that React 18 expects to start hydration on document (therefore the entire HTML document) instead of an element inside <body>.
This means that the boilerplate template for React 18 SSR must also be JSX.
To Do:
Since renderToNodeStream() is deprecated, there is still a need to support renderToPipeableStream() moving forward. There are two important things to do to make this possible:
Support a JSX boilerplate-generator template because hydration now happens on document instead of an HTML element. This requires allowing a custom template to generate the HTML output. Either Meteor adds a custom template for React or allows the developer to add one.
Allow access to the response parameter accessible by renderToPipeableStream()
Ideally, these features will be available through server-render package.
So, I’ve just proven myself wrong. With the solution above, it’s possible to even make this work with class components with data fetching happening in the constructor() when defining the component state variables
I was trying to upgrade my example above to Meteor 3 but was met with package conflicts. I don’t have time to figure out the issue for now so I don’t have an example to share.
We are also in the middle of upgrading our SSR code to Meteor 3 (on hold due to new business requirements we have to prioritize). We are currently moving data fetching as a parent of SSR components to allow suspending the entire component with use()
Please share if you have something to show. I’ll do the same thing once I get the opportunity