Skip to content

Coding practice

Next.js Coding Questions

15 hands-on challenges with prompts and solution sketches for Next.js interview rounds.

Challenge set

15 coding questions

1. App Router Page

tsx

Create a basic app router page.

export default function Page() {
  return <h1>Interview Prep</h1>;
}

2. Server Fetch

tsx

Fetch data in a Server Component.

export default async function Page() {
  const res = await fetch("https://api.example.com/posts", { next: { revalidate: 60 } });
  const posts = await res.json();
  return <pre>{JSON.stringify(posts, null, 2)}</pre>;
}

3. Dynamic Route

tsx

Read params in a dynamic segment.

export default async function Page({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  return <h1>{slug}</h1>;
}

4. Route Handler

typescript

Return JSON from a route handler.

export async function GET() {
  return Response.json({ ok: true });
}

5. Client Component Button

tsx

Mark a client component and handle click.

"use client";
export function LikeButton() {
  return <button onClick={() => console.log("liked")}>Like</button>;
}

6. generateMetadata

typescript

Export dynamic metadata.

export async function generateMetadata({ params }) {
  const { slug } = await params;
  return { title: `${slug} | PHPKINGDOM` };
}

7. Loading UI

tsx

Add a loading.tsx fallback.

export default function Loading() {
  return <p>Loading...</p>;
}

8. Error Boundary File

tsx

Create error.tsx with reset.

"use client";
export default function Error({ reset }: { reset: () => void }) {
  return <button onClick={reset}>Try again</button>;
}

9. Search Params

tsx

Read searchParams in a page.

export default async function Page({
  searchParams,
}: {
  searchParams: Promise<{ q?: string }>;
}) {
  const { q } = await searchParams;
  return <p>Query: {q}</p>;
}

10. Image Component

tsx

Use next/image with sizes.

import Image from "next/image";
<Image src="/hero.jpg" alt="Hero" width={1200} height={630} sizes="100vw" />

11. Link Prefetch

tsx

Navigate with next/link.

import Link from "next/link";
<Link href="/interview-questions/">Interview Questions</Link>

12. Server Action Form

tsx

Submit a form with a server action.

async function save(formData: FormData) {
  "use server";
  console.log(formData.get("title"));
}
export default function Page() {
  return (
    <form action={save}>
      <input name="title" />
      <button type="submit">Save</button>
    </form>
  );
}

13. notFound

typescript

Trigger a 404 for missing content.

import { notFound } from "next/navigation";
if (!post) notFound();

14. Middleware Redirect

typescript

Redirect unauthenticated users in middleware.

import { NextResponse } from "next/server";
export function middleware(request) {
  if (!request.cookies.get("session")) {
    return NextResponse.redirect(new URL("/login", request.url));
  }
}

15. Streaming Suspense

tsx

Stream a slow component with Suspense.

import { Suspense } from "react";
export default function Page() {
  return (
    <Suspense fallback={<p>Loading feed...</p>}>
      <Feed />
    </Suspense>
  );
}