0

Been trying to use react server components, however, I'm stuck at retrieving session data from next auth. The recommended way to do this is getServerSession which throws an error:

Error: Dynamic server usage: force-dynamic

As per docs, I've set dynamic option to force-dynamic, however, the error persists regardless of the value. Interestingly enough, the page loads fine when refreshed on the "server component" route, however, when navigating to it from a different page, it throws an error

import {authOptions} from 'path/to/[...nextauth]'
export const dynamic = 'force-dynamic'
export default async function Page() {
  const session = await getServerSession(authOptions);
  return null;

UPDATE:

If dynamic is left with default value, the error message changes to:

Error: Dynamic server usage: headers

  14 | export default async function Page(...props: any) {
> 15 | const session = await getServerSession(authOptions);
     |                                       ^
  16 | return null;

the behaviour stays the same, meaning it loads fine when on the rsc route, but throws error when navigating to it

Exanubes
  • 133
  • 2
  • 13
  • In the new /app directory, components are server components by default unless you specify "use client"; at the top of the file / at the top of the function. Why is there a need to use this 'force-dynamic'-option? I'm using server components and I've not heard about that option before, but after reading the documentation for it (https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic), I think there are other ways to achieve what you want. – kzi Jul 10 '23 at 06:26
  • @kzi yeah that's just something that I've tried before posting here. I've updated the main post to include an error message when dynamic is left with default value e.g., auto – Exanubes Jul 10 '23 at 10:19
  • For somebody here https://github.com/vercel/next.js/issues/50634 it worked to use `export const dynamic = "force-static";` for static pages. Maybe this is worth a try. – kzi Jul 10 '23 at 14:16
  • First you asked why I used it then you suggest I use it to fix the issue. I don't get you.... – Exanubes Jul 11 '23 at 12:57
  • Please look at the statement again, it's slightly different than the one, you use. But I don't know if that will help you. The cause of the error may be somewhere in the code that you didn't post. Your usage of getServerSession looks correct as far as I can say. Edit: I saw, you could solve your issue, glad to know. – kzi Jul 11 '23 at 16:11
  • Yeah you're right, didn't notice it was different. My bad, thanks for the help – Exanubes Jul 20 '23 at 17:42

1 Answers1

0

So, as it turns out, the issue was 'caused by another server component which sets up my next-intl and uses the generateStaticParams function. Once I set it to undefined it worked fine.


function getStaticParams(){...}

export const generateStaticParams = process.env.NODE_ENV === 'production' ? getStaticParams : undefined
Exanubes
  • 133
  • 2
  • 13