Next.js Discord

Discord Forum

getServerSideProps Alternative on App Router

Answered
Khao (Ping on reply) posted this in #help-forum
Open in Discord
Avatar
My Code:
import { FaServer, FaEthernet, FaMicrochip, FaMemory, FaFloppyDisk } from 'react-icons/fa6';
import humanFormat from 'human-format';
import Link from 'next/link';
import SiteNavbar from '@/components/SiteNavbar';
import SiteSidebar from '@/components/SiteSidebar';
import config from '@/config';
import { Server } from '@/types';
import type { InferGetServerSidePropsType, GetServerSideProps } from 'next'

export const getServerSideProps = (async () => {
  const res = await fetch(config.Wings.URL + '/api/servers', {
    headers: {
      Authorization: config.Wings.Token,
    },
  });
  const servers: Server[] = await res.json()

  return { props: { servers } }
}) satisfies GetServerSideProps<{ servers: Server[] }>

export default function Page({
  servers,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
  const formatSize = (size: number) => humanFormat(size, { scale: humanFormat.Scale.create(["Bytes", "KB", "MB", "GB", "TB"], 1024) });
  return <SiteSidebar serverView={false}>
    <SiteNavbar />
    <div className='p-6 px-12 items-center flex flex-col space-y-2 menu menu-vertical'>
      {servers ? (servers.length == 0 
        ? <h1>There are no servers associated with your account.</h1> 


Now I got an error saying getServerSideProps aren't working on App Router but now I don't know what options I have.
Answered by averydelusionalperson
create a different component for button?
View full answer

57 Replies

Avatar
it's a server component, you can just fetch in the Page component
it will be run server side
check docs on server component
Avatar
I tried and it kept complaining about async/await in a Client Component
Layout
export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={inter.className}>
        <AuthProvider>
          {children}
        </AuthProvider>
      </body>
    </html>
  );
}
Avatar
you can't use client things in the server component. like onClick, onHover, or useState. etc
it's pure server
you get the data in server
render the data
and return it to browser
Avatar
Nah the AuthProvider is a client component
Avatar
is it next-auth?
Avatar
Yup
v5
beta thing
?
Avatar
it shouldn't matter ig?
is your component server or client?
Avatar
'use client';

import { SessionProvider } from 'next-auth/react';
export default function AuthProvider({
  children,
}: {
  children?: React.ReactNode;
}) {
  return <SessionProvider>{children}</SessionProvider>;
}
okay now looking at that
it looks useless
Avatar
yeah
is this your Page, server or client?
in which you're trying to get data
Avatar
that is AuthProvider.tsx
a component
and then this is the layout.tsx
this is the page
I just want it so i can grab the servers and grab the session data from next-auth
here is the navbar component which uses the auth
'use client';
import { auth } from "@/auth";
import Image from "next/image";
import { FaMagnifyingGlass } from "react-icons/fa6";

export default async function SiteNavbar() {
    const session = await auth();
    return <div className="navbar bg-base-100 px-12">
        <div className="navbar-start" />
        <div className="navbar-center px-2">
            <label htmlFor="my-drawer-2" className="btn btn-primary drawer-button lg:hidden">Open Sidebar</label>
        </div>
        <div className="navbar-end">
            <div className='bg-base-200 items-center flex space-x-4 p-1 px-4 rounded-xl'>
                <div className="avatar">
                    <div className="w-12 rounded-lg">
                        <Image src={session?.user?.image ?? 'https://www.gravatar.com/avatar'} unoptimized={true} priority alt='Avatar' width={12} height={12} />
                    </div>
                </div>
                <span className='font-bold text-white'>{session?.user?.name ?? 'John Doe'}</span>
            </div>
        </div>
    </div>
}
Avatar
bump
Avatar
@averydelusionalperson ?
Avatar
Pyramid ant
make ur function async
your component*
Avatar
it is?
Avatar
Pyramid ant
what does it say
Avatar
2secs
okay apparently my code broke overnight so imma fix it and let you know the results
Avatar
imma be honest, I'm not clear what you want
Avatar
2secs
Avatar
K, a little off-topic but

import { auth } from "@/auth";
import Image from "next/image";
import { FaMagnifyingGlass } from "react-icons/fa6";

export default async function SiteNavbar() {
    const session = await auth();
    return <div className="navbar bg-base-100 px-12">
        <div className="navbar-start">
            <button className="btn btn-sm w-full max-w-md justify-between" onClick={() => (document.getElementById('search_modal') as any).showModal()}>
                <span>Search</span>
                <FaMagnifyingGlass />
            </button>


I want to use
const session = await auth();

which requires it to be a Server Component but now
<button className="btn btn-sm w-full max-w-md justify-between" onClick={() => (document.getElementById('search_modal') as any).showModal()}>

Wants a Client Component, how would I workaround this?
Avatar
create a different component for button?
Answer
Avatar
client component
Avatar
Alright thanks
Avatar
sure
so, is this thread closed or what?
Avatar
I think it is yeah
not sure which thing should be the solution
this?
Avatar
oh, you can choose any message and mark it as solution, I don't think we found any solution tho.
It's upto you
🫡
cya soon
Avatar
soon
yeah cya soon
Avatar
lol
Avatar
bro legit has no faith in me