Lesson 9 of 10
Practice & Quiz - Next.js & Deployment
Capstone quiz covering Next.js App Router, RSC vs Client Components, Server Actions, ISR, SEO metadata, and Vercel deployment with a practical exercise and 5 questions.
Practical Exercise - Next.js Blog with ISR & Server Actions
// ─── app/blog/[slug]/page.tsx ─────────────────────────────────
// Server Component (default in App Router) - no 'use client'
import { notFound } from 'next/navigation';
import type { Metadata } from 'next';
interface Post { slug: string; title: string; content: string; views: number; }
async function getPost(slug: string): Promise<Post | null> {
const res = await fetch(`https://api.example.com/posts/${slug}`, {
// ISR: revalidate every 60 seconds
next: { revalidate: 60 },
});
if (res.status === 404) return null;
if (!res.ok) throw new Error('Failed to fetch post');
return res.json();
}
// Dynamic metadata - generated on the server per request
export async function generateMetadata(
{ params }: { params: { slug: string } }
): Promise<Metadata> {
const post = await getPost(params.slug);
if (!post) return { title: 'Post Not Found' };
return {
title: post.title,
description: post.content.slice(0, 155),
openGraph: { title: post.title, type: 'article' },
};
}
// Static paths for top posts - rest generated on demand
export async function generateStaticParams() {
const res = await fetch('https://api.example.com/posts?limit=10');
const posts: Post[] = await res.json();
return posts.map(p => ({ slug: p.slug }));
}
// ─── Server Action - runs on the server, called from client ───
async function incrementViews(slug: string) {
'use server';
await fetch(`https://api.example.com/posts/${slug}/views`, {
method: 'POST',
// Revalidate cached data after mutation
cache: 'no-store',
});
// revalidatePath and revalidateTag available here
}
// ─── Client Component - interactive part only ─────────────────
'use client';
import { useOptimistic, useTransition } from 'react';
function ViewCounter({ initialViews, slug }: { initialViews: number; slug: string }) {
const [isPending, startTransition] = useTransition();
const [optimisticViews, addOptimisticView] = useOptimistic(
initialViews,
(state) => state + 1
);
return (
<button
onClick={() => startTransition(async () => {
addOptimisticView(null); // optimistic update immediately
await incrementViews(slug); // server action
})}
disabled={isPending}
>
👁 {optimisticViews} views
</button>
);
}
// ─── Page Server Component - composes server + client ─────────
export default async function BlogPost({ params }: { params: { slug: string } }) {
const post = await getPost(params.slug);
if (!post) notFound();
return (
<article>
<h1>{post.title}</h1>
<ViewCounter initialViews={post.views} slug={post.slug} />
<div dangerouslySetInnerHTML={{ __html: post.content }} />
</article>
);
}Capstone Quiz
In Next.js App Router, what is the default rendering mode for a component with no 'use client' directive?
Tip
Tip
Practice Practice Quiz Nextjs Deployment in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Modern React: Use Hooks instead of class lifecycle methods
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz Nextjs Deployment from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Practice Quiz Nextjs Deployment is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?