Next.js Discord

Discord Forum

Issues rendering SSR.

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Hi!

I'm having some issues getting SSR to work in a real project.

My folderlayout is as follows;
Answered by Ray
if you want to use app router, the file path should be app/[formationId]/page.tsx
View full answer

27 Replies

Asiatic LionOP
This is my component

'use client';

import { FormationObject } from '@/app/Types/FormationType';
import FormationSettings from '@/app/components/FormationSpecific/FormationSettings';
import PlayerInstructions from '@/app/components/FormationSpecific/PlayerInstructions';
import { fifaFormationsDb } from '@/app/layout';
import { Stack, Typography } from '@mui/material';
import { doc, getDoc } from 'firebase/firestore';

interface PageProps {
    data: FormationObject;
    error?: string;
}

export default function Page({ data, error }: PageProps) {
    if (error || !data) {
        return <Typography>Error: {error || 'Not found'}</Typography>;
    }

    return (
        <Stack spacing={2} alignItems={'center'}>
            <FormationSettings formation={data} />
            <PlayerInstructions formation={data} />
        </Stack>
    );
}

export async function getServerSideProps(context: any) {
    const formationId = context.params.formationId;
    console.log(formationId);
    try {
        const docRef = doc(fifaFormationsDb, 'formations', formationId);
        const docSnap = await getDoc(docRef);
        return { data: docSnap.data() as FormationObject };
    } catch (error: any) {
        console.error(error.message);
        return { error: error.message };
    }
}
When i try to visit localhost/formationId i simply get a 404 for some reason. I tried to get this working by creating a pages folder, and within it a [formationId] folder, and within that one i have my page.tsx
Answer
and getServerSideProps is available in page router only
Asiatic LionOP
Is it recommended to mix app/page router?
I got it to work, but i noticed my "overlay" (header etc) isnt available from pages router, so i suspect i did the intial setup using app router. Would it be best to convert everything to page router?
if you are new to nextjs, I would suggest you learn app router first
Asiatic LionOP
I'm very new, but i specifically choose nextjs for the SSR-component of it
so maybe pages? ;P
app router
Asiatic LionOP
But i cant use getServerSideProps if i use app?
I have tons of pages with dynamically rendered user content
in app router, you can just do whatever you do in getServerSideProps inside the component
therefore we don't need getServerSideProps in app router
Asiatic LionOP
But if i just made components like if it was using react i wont get the SEO benefits of ssr, right?
export default async function Page() {
  const res = await fetch('https://api.example.com/...')
  const data = await res.json()
  return <main></main>
}
why not
it is ssr by default
Asiatic LionOP
Oh....
Welp
client component will also render on server
Asiatic LionOP
Thats interesting
48 hours of trial and error in vain
:lolsob: :lolsob:
read the doc
this is good course for you