SSR
Server-Side Rendering
A rendering method that generates HTML on the server and sends it. Good for SEO. Next.js and Nuxt.js are typical.
Server-Side Rendering
A rendering method that generates HTML on the server and sends it. Good for SEO. Next.js and Nuxt.js are typical.
SSR (Server-Side Rendering) is a rendering method in which the server dynamically generates HTML and sends it each time a user requests a page. Because the client receives complete HTML that already contains the content, the screen is displayed quickly on initial load.
The biggest advantages of SSR are SEO and social media sharing. Search engine crawlers and social media bots can read the page content without executing JavaScript, which solves the SEO problem of SPAs. Meta tags can also be generated dynamically.
Next.js's getServerSideProps, Nuxt's asyncData, Remix's loader, and others implement SSR. After data is fetched on the server and included in the HTML, the client activates it as a React/Vue app through Hydration.
SSR increases server load and has a slower TTFB (Time To First Byte) than SSG. However, it is essential when real-time data is needed or when user-specific content must be provided. Response time can be improved with Streaming SSR.
// Next.js SSR with getServerSideProps
// pages/dashboard.tsx
import { GetServerSideProps } from 'next';
import { getServerSession } from 'next-auth';
import { authOptions } from './api/auth/[...nextauth]';
interface DashboardProps {
user: { name: string; email: string };
stats: { visitors: number; revenue: number };
}
// λ§€ μμ²λ§λ€ μλ²μμ μ€ν
export const getServerSideProps: GetServerSideProps = async (context) => {
// μΈμ¦ νμΈ
const session = await getServerSession(context.req, context.res, authOptions);
if (!session) {
return {
redirect: {
destination: '/login',
permanent: false,
},
};
}
// μ€μκ° λ°μ΄ν° νμΉ
const [user, stats] = await Promise.all([
fetch(`${process.env.API_URL}/user/${session.user.id}`).then(r => r.json()),
fetch(`${process.env.API_URL}/stats`, {
headers: { Authorization: `Bearer ${session.accessToken}` }
}).then(r => r.json())
]);
return {
props: {
user,
stats,
},
};
};
export default function Dashboard({ user, stats }: DashboardProps) {
return (
<div>
<h1>μλ
νμΈμ, {user.name}λ</h1>
<div className="stats">
<div>λ°©λ¬Έμ: {stats.visitors.toLocaleString()}</div>
<div>λ§€μΆ: β©{stats.revenue.toLocaleString()}</div>
</div>
</div>
);
}
// Next.js App Router SSR
// app/dashboard/page.tsx
async function Dashboard() {
const session = await getServerSession();
const stats = await fetch('https://api.example.com/stats', {
cache: 'no-store' // SSR κ°μ
}).then(r => r.json());
return <DashboardClient stats={stats} />;
}
In a technical meeting:
"The dashboard page is right for SSR. It needs real-time data and authentication too. But if you're worried about server load, you can add a caching layer or set the stale-while-revalidate header."
In a technical interview:
"What are the drawbacks of SSR and how do you address them?" - "TTFB can get slower, but you can render progressively with Streaming SSR, or run it on the Edge Runtime to reduce latency."
In a code review:
"You're calling 5 APIs sequentially in getServerSideProps. If you parallelize them with Promise.all, you can cut the response time by more than half. Send requests that have no dependencies at the same time."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free