#Using server components to generate tokens

1 messages · Page 1 of 1 (latest)

stuck forumBOT
#

Hi @dreamy axle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can write server-side code inside Server Components. When you run next build, Server Components consumed inside the app directory will run during the build, similar to traditional static-site generation. The resulting component will be rendered into static HTML for the initial page load and a static payload for client navigation between routes source (https://nextjs.org/docs/app/building-your-application/deploying/static-exports#server-components).

For example, you can fetch data from an API and use it in your Server Component:

export default async function Page() {
  // This fetch will run on the server during `next build`
  const res = await fetch('https://api.example.com/...');
  const data = await res.json();
  
  return <main>...</main>;
}

However, the provided code snippet uses the StreamChat library, which is not mentioned in the information extracts. Therefore, I cannot provide a confident answer on how to use it within a Server Component.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Next.js enables starting as a static site or Single-Page Application (SPA), then later optionally upgrading to use features that require a server.