Server-side Rendering & Static Generation
Next.js supports Server-side Rendering (SSR) and Static Site Generation (SSG) out of the box for pages. However, with Next.js you are responsible for defining getStaticProps or getServersideProps, fetching the necessary data, and providing it on props. Faust.js provides two functions that can be used for SSG and SSR respectively called getNextStaticProps and getNextServerSideProps.
SSG Using getNextStaticProps#
This helper function lets you build a static site with your WordPress data. The function should be returned from getStaticProps:
The function accepts two arguments: the static props context, and an object with a Page key and client key. This should be your Next.js page component:
The reason MyPage and client are passed to getNextStaticProps is because under the hood Faust.js perform a skeleton render of the page component to know what data to fetch and what queries to build. The flow is as follows:
- The passed in
clientis used for all requests. - A skeleton render of the
Pagecomponent is invoked, withnext/routercontext and the properclientfor making requests provided. - After rendering, the
clientcache is serialized and stored onprops. - The
propsare returned in the standard Next.js format, withrevalidate: 1
This allows the developer to not have to think about batching/constructing queries, or data fetching. You are able to write your page as if you will be using Client-side Rendering (CSR). Then, you add the getStaticProps function above and can take advantage of SSG!
SSR Using getNextServerSideProps#
This helper function lets you server side render your page with WordPress data. The function should be returned from getServerSideProps:
The function accepts two arguments: the server side props context, and an object with a Page key. This should be your Next.js page component:
As mentioned in getNextStaticProps, the reason MyPage and client are passed to getNextServerSideProps is because under the hood Faust.js performs a skeleton render of the page component to know what data to fetch and what queries to build. This allows the developer to not have to think about batching/constructing queries, or data fetching.
Rehydration Using <HeadlessProvider />#
In order to properly facilitate SSR and SSG you must use the built-in component published in faustjs/next called HeadlessProvider. This component performs the following:
- Sets the
clientto be used with every request for WordPress data. - Hydrates the
clientcache using the prepared cache snapshot fromgetNextServerSidePropsorgetNextStaticProps. - Renders its
children
Adding <HeadlessProvider /> to your _app.tsx#
Using the HeadlessProvider component us easy, and if you are using an example next project published by Faust.js it will happen automatically. If you are adding Faust.js to your project, you will want to put HeadlessProvider at the top of your component tree. Typically in a Next.js app this means in your pages/_app.tsx file as follows: