React Server-Side Rendering
#
HelpersThese functions are meant to be used together, in different steps of your application.
#
prepareReactRenderFunction designed to be used before the React Render is being made, for example, in places like getStaticProps or getServerSideProps of Next.js
Internally it uses react-ssr-prepass
#
useHydrateCacheFunction designed to be placed at the very top of the hydrated component
#
ExampleMade with Next.js SSR Data Fetching in mind.
It's exactly the same usage for
getServerSideProps
If you are using
staleWhileRevalidate
in your application, You should always set 'shouldRefetch' as false, since that feature will always do the refetch anyways.
#
Using SuspensePlease check Suspense in SSR
#
Using Next.js RouterIf you are using Next.js useRouter
inside a component of a Next.js project, you might encounter errors like:
TypeError: Cannot destructure property 'pathname' of 'Object(...)(...)' as it is null.
Since with prepareReactRender
you are rendering outside of the Next.js tree, you don't have access to it's router
, and it's not even created yet.
We suggest a couple of solutions:
#
Add a default object before destructuringYou will need to check if you really have any router property available
#
Mocking Router DataHere you can either manually set the values of the router, or simply let it be {}
, and all it's properties are simply going to be undefined.