Next.js is a React framework that enables server-side rendering (SSR) and static site generation (SSG) along with hybrid applications, offering optimized performance and SEO compared to traditional React apps that use client-side rendering (CSR)1.
- Client-Side Rendering (CSR): React apps render everything on the client side (browser).
- Cons:
Next.js solves these issues by enabling Server-Side Rendering (SSR), Static Site Generation (SSG), and Incremental Static Regeneration (ISR)1.
- Server-Side Rendering (SSR): Content is pre-rendered on the server. When a bot or user requests a page, they get fully rendered HTML.
- SEO: Fully rendered content is visible to bots and search engines6.
- Performance: SSR enables a faster time to first contentful paint3.
- Routing: Next.js has a built-in router that is file-based7.
- File-based routing: Next.js uses the file structure to define routes7.
- Example: If you create a file at
pages/tree/branch.js
, you can access it viaexample.com/tree/branch
.
- Example: If you create a file at
// pages/about.tsx
const About = () => {
return (
<div>
<h1>About Us</h1>
<p>We are a Next.js powered application.</p>
</div>
);
};
export default About;
Visit /about
at example.com/about
.
Next.js pre-renders every page by default, which means the HTML is generated in advance. There are two forms of pre-rendering1:
- Pages are generated at build time and served as static files. This is ideal for pages that don't change often, like a blog or marketing site8.
- Example: Use
getStaticProps()
to fetch data at build time.
// pages/index.tsx
export async function getStaticProps() {
const data = await fetchSomeData();
return { props: { data } };
}
const HomePage = ({ data }) => {
return <div>{data}</div>;
};
export default HomePage;
- Pages are generated at request time, meaning the server will generate fresh HTML every time a request is made4.
- Example: Use
getServerSideProps()
to fetch data on each request.
// pages/profile.tsx
export async function getServerSideProps() {
const userData = await fetchUserData();
return { props: { userData } };
}
const ProfilePage = ({ userData }) => {
return <div>{userData.name}</div>;
};
export default ProfilePage;
- Pages can be updated after the site is built without rebuilding the entire site9.
- Example: Use
revalidate
to regenerate the page in the background.
// pages/blog/[id].tsx
export async function getStaticProps({ params }) {
const blogPost = await fetchBlogPost(params.id);
return {
props: { blogPost },
revalidate: 10, // Revalidate this page every 10 seconds
};
}
You can create API endpoints using file-based routing. These are placed in the pages/api/
folder and can handle requests, such as fetching data or sending responses10.
// pages/api/hello.ts
export default function handler(req, res) {
res.status(200).json({ message: 'Hello, Next.js!' });
}
Visit /api/hello
to see the API response.
Next.js allows you to:
- Pre-render content for better SEO and performance6.
- Serve pages dynamically with server-side rendering or build static pages with static generation8.
- Use file-based routing for easier project organization7.
- Seamlessly handle client-side and server-side logic1.
For more advanced topics: