I am trying to code splitting and lazy loading in my website but I am getting an error about Suspense boundary.
Error:
Uncaught Error: The server did not finish this Suspense boundary: The server used "renderToString" which does not support Suspense. If you intended for this Suspense boundary to render the fallback content on the server consider throwing an Error somewhere within the Suspense boundary. If you intended to have the server wait for the suspended component please switch to "renderToPipeableStream" which supports Suspense on the server.
Info:
I am using GatsbyJS 4.24.1
and React 18.2.0
.
I get the error with the production build or in development mode with DEV_SSR: true
.
I read a lot but I got the idea GatsbyJS fully support Lazy loading and Suspense, so I do not know if I am doing something wrong or it is just a bug of Gatsby (maybe a regression?). I already post an issue on Gatsby (https://github.com/gatsbyjs/gatsby/issues/36678), but still no answer.
Example:
Looking around I do not think I am doing something wrong. It is just
const Hero = lazy(() => import('../components/home/Hero'));
function Home() {
return (
<Layout>
<Suspense fallback={null}>
<Hero />
</Suspense>
</Layout>
);
}
Minimal reproduction:
I also created a codesanbox: https://codesandbox.io/s/determined-violet-cs7bxh. You can also find another live example here: https://gatsbysuspensereproduction.gtsb.io/, and the code here: https://github.com/PaulieScanlon/gatsby-suspense-reproduction
Ideal solution:
Looking for a fix (so I am doing something wrong) or a bug confirm (so I can move on and try to migrate migrate to nextjs to overcome the issue, or still with GatsbbyJS but downgrade to React 17
and @loadable/components
).