next-ssr-fallback
v1.0.0
Published
Make it easier for Next.js app to support SSR fallback to CSR
Downloads
13
Readme
next-ssr-fallback
This library helps you to fallback to CSR more easily when using SSR to avoid Next.js app rendering failure.
Introduction
We usually use getServerSideProps
in Next.js to fetch the initial properties of SSR. If the acquisition fails due to server-side network problems, Next.js will return a status code of 500, which causes our app to fail to render normally.
In order to solve this problem, we need to add the fallback logic by CSR, and obtain properties on the client again when SSR acquisition fails, so as to improve the success rate of rendering.
This library is to help you do this, it only takes a few lines of code to complete.
Supported Versions
next-ssr-fallback
is tested with:
- next:
^12.0.0
and above
Installation
First, install next-ssr-fallback
:
$ npm install next-ssr-fallback
# OR
$ yarn add next-ssr-fallback
# OR
$ pnpm install next-ssr-fallback
Usage
You need to make the following changes to the page components:
- rename the
getServerSideProps
function togetServerSidePropsOrigin
- import
next-ssr-fallback
and create a newSSRFallback
instance
import FallbackSSR from 'next-ssr-fallback';
const fallbackSSR = new FallbackSSR({
getServerSideProps: getServerSidePropsOrigin,
});
- modify the export of
getServerSideProps
by usingcreateGetServerSidePropsFunction
export const getServerSideProps = fallbackSSR.createGetServerSidePropsFunction();
- Use higher-order component wrappers
withCSR
for page components
export default fallbackSSR.withCSR(PageComponent);
That's all there is to it, your page will support fallback to CSR.
Example
Here is a example recruit-pc using this library. You can refer to how to use.