Closed WillSmithTE closed 2 months ago
I think you just need to add a Suspense boundary around the lazy component.
Server-side, the import
can be resolved synchronously but then during hydration client-side, the module needs to be downloaded for the lazy component so React can't hydrate the with same UI the server-rendered and needs to suspend.
In my local testing, adding a suspense boundary resolves the hydration issue. Can you confirm this fixes your setup as well?
True, that will do, thanks!
I wanted to just "wait" to render while importing, but now you put it like that, I guess that doesn't make sense in the ssr -> hydration -> client render lifecycle, right?
yeah not really - generally you either (1) render the component during SSR and include it as a critical path import so it can hydrate, or (2) use suspense to render a spinner during SSR and then you can hydrate the spinner while the client fetches the lazy component.
If you really wanted to render the component during SSR and then delay hydration until the client had downloaded the component, you'd have to load the component ahead of hydrating your app in entry.client - but I wouldn't recommend this since the app will be unresponsive during that time
import('./lazy').then(() => {
hydrateRoot(
document,
<StrictMode>
<RemixBrowser />
</StrictMode>
);
});
makes sense, thanks. the app used to be a spa with a bunch of heavy components/imports for only certain users, so lazy let me have no CLS. i'll have to work out which way the ux should go now that it could be a SPA render or SSR
Reproduction
Github
Deployed
I tried to create a stackblitz but failed, maybe needs node 20? Dunno
Create a new project with the official cloudflare starter (
npm create cloudflare@latest my-remix-app -- --framework=remix
).Add a lazy-loaded component
System Info