next-with-gqless-example
This app is an example of how to use gqless with Next.js. It mirrors the with-apollo Next.js example app. Apollo examples are included here for comparison.
The Gist
- Copy boilerplate code from ./lib/gqless.js (including
withGqless
&useGqless
) into your project- Copy the file
- Install the required
react-ssr-prepass
package (actually@zen_flow/react-ssr-prepass
until this patch is released). - Add the required
getClient
function togqless/client.ts
.export const createClient = (queryFetcher = fetchQuery) => new Client<Query>(schema.Query, queryFetcher);
- Use the
withGqless
HOC on the Next.js pages you want SSR'd. This will add a React context provider for the gqless client and wrapgetInitialProps
to prefetch the necessary data before displaying the page. - Use the
useGqless
hook to access the gqlessclient
(andquery
) from context (in components that will be SSR'd). We cannot use the singletonclient
(orquery
) exported fromClient.ts
for SSR because on the server we want to have one client per request. - Use
SsrCompatibleSuspense
instead of the regular ReactSuspense
component. This will stop ReactDOMServer from complaining and crashing when<Suspense/>
is encountered during SSR. The caveat here is that React will log warnings when rehydrating browser-side, since what's rendered on browser is supposed to match what was rendered on the server. (Issue #2)