Focus: General (Conceptual, Coding, and Practical Scenarios)
Next.js uses File-Based Routing:
pages directory becomes a route (pages/index.js → /). Routes**:
pages/blog/[slug].js → /blog/:slugpages/blog/[...slug].js → /blog/a/b/c| Method | When to Use | Key Features |
|---|---|---|
getStaticProps | Static content (pre-rendered at build) | Returns props for the page; uses getStaticPaths for dynamic routes. |
getServerSideProps | Data that changes frequently | Runs on every request; returns props or revalidate for ISR. |
getStaticPaths | Pre-render dynamic routes | Specifies paths to pre-render at build time. |
| Client-Side Fetching | User-specific data | Use useEffect or libraries like swr/react-query. |
pages/api/*.pages/api/hello.js → POST /api/helloexport default function handler(req, res) { res.status(200).json({ message: "Hello from API!" }); }
export { default } from 'next-middlewares/with-clerk-auth';
app/):
app/layout.js (root layout), app/page.js (main page).app/blog/[slug]/page.js → /blog/:slug.page.js exports loader and action for server-side logic.Answer:
getServerSideProps.getStaticProps + getStaticPaths.revalidate in getStaticProps (e.g., revalidate: 60 → re-generate every 60s).Answer:
next/dynamic).next/image for automatic resizing, format conversion, and lazy loading.prefetch in next/link to preload data.next/font: Self-hosted, optimized fonts.Code Example:
import Image from 'next/image'; export default function Page() { return ( <Image src="/path/to/image.jpg" alt="Description" width={500} height={300} priority // Loads immediately /> ); }
Answer:
Approach 1: Middleware
next-auth.js or custom middleware to protect routes.// middleware.js import { withAuth } from 'next-auth/middleware'; export default withAuth({ callbacks: { authorized: ({ token }) => !!token, }, });
Approach 2: Session-Based (Client-Side)
getServerSideProps:export async function getServerSideProps(context) { const { req } = context; const session = await getSession({ req }); if (!session) return { redirect: { destination: '/login' } }; return { props: {} }; }
// pages/blog/[slug]/page.js export async function getStaticProps({ params }) { const post = await fetch(`https://api.example.com/posts/${params.slug}`); return { props: { post }, revalidate: 3600 }; // Re-validate every hour } export async function getStaticPaths() { const posts = await fetch('https://api.example.com/posts'); const paths = posts.map(post => ({ params: { slug: post.id } })); return { paths, fallback: 'blocking' }; // Show HTML while generating }
next/api for Simple CRUD// app/api/todo/[id]/route.js (App Router) import { NextResponse } from 'next/server'; export async function GET({ params }) { const todo = await db.todo.findUnique({ where: { id: params.id } }); return NextResponse.json(todo); } export async function DELETE({ params }) { await db.todo.delete({ where: { id: params.id } }); return NextResponse.json({ success: true }); }
swr, and integration with CMS-headless (e.g., Strapi, Sanity).Next Steps:
Start a new session to explore different topics or increase the difficulty level.
Start New Session