BasicsVSCodeNext.jsStyled ComponentsSetupProjectsResourcesIndex
Next.js Hero

Next.js

Your framework for modern web apps

Next.js Essentials

Getting Started

Purpose:

Set up a Next.js project to build fast, modern web applications with React.

Use Case:

Use this when starting a new project or learning the basics of Next.js for server-side rendering and static sites.

Example:

Initialize a Next.js app in your terminal:

npx create-next-app@latest my-next-app
cd my-next-app
npm run dev
Pro Tip:

Choose TypeScript during setup by adding '--typescript' to the command for better type safety.

Dive Deeper

Core Features

Next.js shines with file-based routing, server-side rendering (SSR), and static site generation (SSG). Routing is automatic based on your app folder, SSR delivers dynamic content, and SSG pre-renders pages for performance.

File-Based Routing

Purpose:

Create pages effortlessly using Next.js’s file-based routing system.

Use Case:

Ideal for quickly setting up routes like /about or /blog without configuring a router.

Example:

Add a file named page.tsx in app/about/:

// app/about/page.tsx
'use client';

export default function About() {
  return <h1>About Page</h1>;
}
Pro Tip:

Use dynamic routes with [id]/page.tsx for pages like /post/1—check the docs for params handling.

Dive Deeper

Server-Side Rendering (SSR)

Purpose:

Render pages on the server for SEO and faster initial loads with dynamic data.

Use Case:

Use SSR when you need fresh data on each request, like a news feed or user dashboard.

Example:

Fetch data in getServerSideProps:

// app/page.tsx
'use client';

export async function getServerSideProps() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return { props: { data } };
}
export default function Page({ data }) {
  return <div>{data.title}</div>;
}
Pro Tip:

Keep getServerSideProps lightweight—fetch only what’s needed to avoid slow load times.

Static Site Generation (SSG)

Purpose:

Pre-render pages at build time for maximum performance and scalability.

Use Case:

Perfect for static content like blogs, documentation, or landing pages that don’t change often.

Example:

Use getStaticProps for SSG:

// app/page.tsx
'use client';

export async function getStaticProps() {
  const res = await fetch('https://api.example.com/posts');
  const posts = await res.json();
  return { props: { posts } };
}
export default function Blog({ posts }) {
  return <ul>{posts.map(post => <li key={post.id}>{post.title}</li>)}</ul>;
}
Pro Tip:

Combine SSG with revalidation (revalidate: 10) to refresh static pages periodically.

Advanced Features

API Routes

Purpose:

Build backend endpoints within your Next.js app using API routes.

Use Case:

Use this for simple APIs, like form submissions or data fetching, without a separate server.

Example:

Create app/api/hello/route.ts:

// app/api/hello/route.ts
export async function GET(req) {
  return new Response(JSON.stringify({ message: 'Hello from Next.js!' }), { status: 200 });
}
Pro Tip:

Test your API route locally at http://localhost:3000/api/hello after running npm run dev.

Dive Deeper

Dynamic Routes

Purpose:

Handle dynamic URLs like /post/[id] with Next.js’s dynamic routing.

Use Case:

Great for blog posts, product pages, or anything with variable slugs.

Example:

Create app/post/[id]/page.tsx:

// app/post/[id]/page.tsx
'use client';

export async function getStaticPaths() {
  return { paths: [{ params: { id: '1' } }], fallback: false };
}
export async function getStaticProps({ params }) {
  const post = { id: params.id, title: 'Post ' + params.id };
  return { props: { post } };
}
export default function Post({ post }) {
  return <h1>{post.title}</h1>;
}
Pro Tip:

Use fallback: 'blocking' in getStaticPaths for on-demand static generation of new paths.

Workflow Enhancements

Next.js boosts development with automatic code splitting, image optimization, and TypeScript support. Code splitting reduces bundle size, <Image> optimizes assets, and TypeScript adds type safety right out of the box.

Image Optimization

Purpose:

Use Next.js’s <Image> component to automatically optimize and lazy-load images.

Use Case:

Perfect for improving performance on image-heavy pages like portfolios or galleries.

Example:

Add an image to your page:

// app/page.tsx
'use client';

import Image from 'next/image';

export default function Home() {
  return <Image src="/my-image.jpg" alt="Example" width={500} height={300} />;
}
Pro Tip:

Set priority on key images above the fold to prioritize their loading.

TypeScript Integration

Purpose:

Leverage TypeScript in Next.js for better code quality and developer experience.

Use Case:

Use this for large projects or teams where type checking prevents bugs.

Example:

Define a typed component in app/page.tsx:

// app/page.tsx
'use client';

type Props = { message: string };

export default function Home({ message }: Props) {
  return <h1>{message}</h1>;
}
export async function getStaticProps() {
  return { props: { message: 'Hello, Layer!' } };
}
Pro Tip:

Run npm install --save-dev typescript @types/react @types/node if you didn’t start with TypeScript.

Dive Deeper
Chatbot

...

Welcome to the Prompted Chatbot! How can I assist you today?