RevereCRE / relay-nextjs

⚡️ Relay integration for Next.js apps
https://reverecre.github.io/relay-nextjs/
MIT License
251 stars 30 forks source link

Use with other server side props #62

Closed urgent closed 2 years ago

urgent commented 2 years ago

next-auth requires I pass a session prop from the server on page load. Otherwise there's a delay as a call is needed to be made client side. Is it possible to provide a server variable alongside relay-nextjs?

function Home({ preloadedQuery, session }) {
...
}

export default withRelay(Home, HomeQuery, {
...
})

export async function getInitialProps(context) {
  return {
    props: {
      session: await getSession(context),
    },
  };
}
rrdelaney commented 2 years ago

Yes, this is possible with the serverSideProps option in the page API: https://reverecre.github.io/relay-nextjs/docs/page-api#relayoptions.

Example usage would be something like:

export default withRelay(Home, HomeQuery, {
  // ...
  serverSideProps: async (context) => {
    const { getSession } = await import('my-auth-library');
    const session = await getSession(context);
    return { session };
  },
})