Static Site Generation (SSG) is a method of pre-rendering pages at build time, which can significantly improve the performance and SEO of your web application. Next.js, a popular React framework, provides robust support for SSG, making it easier to build static websites.
Key Concepts
-
Static Site Generation (SSG):
- Pre-renders pages at build time.
- Generates HTML for each page and serves it on request.
- Ideal for pages that do not change frequently.
-
Next.js:
- A React framework for building server-side rendered (SSR) and statically generated (SSG) applications.
- Provides features like file-based routing, API routes, and more.
-
getStaticProps:
- A Next.js function used to fetch data at build time.
- Returns an object with props that are passed to the page component.
-
getStaticPaths:
- A Next.js function used to specify dynamic routes to be pre-rendered based on data.
- Returns an object with paths and fallback options.
Setting Up a Next.js Project
-
Install Next.js:
npx create-next-app my-nextjs-app cd my-nextjs-app npm run dev
-
Project Structure:
pages/
: Contains all the page components.public/
: Static assets like images.styles/
: CSS files.
Example: Building a Blog with SSG
Step 1: Create a Blog Page
- Create a new file
pages/blog.js
:import React from 'react'; const Blog = ({ posts }) => { return ( <div> <h1>Blog</h1> <ul> {posts.map((post) => ( <li key={post.id}>{post.title}</li> ))} </ul> </div> ); }; export async function getStaticProps() { const res = await fetch('https://jsonplaceholder.typicode.com/posts'); const posts = await res.json(); return { props: { posts, }, }; } export default Blog;
Step 2: Create Dynamic Routes for Blog Posts
- Create a new file
pages/blog/[id].js
:import React from 'react'; const Post = ({ post }) => { return ( <div> <h1>{post.title}</h1> <p>{post.body}</p> </div> ); }; export async function getStaticPaths() { const res = await fetch('https://jsonplaceholder.typicode.com/posts'); const posts = await res.json(); const paths = posts.map((post) => ({ params: { id: post.id.toString() }, })); return { paths, fallback: false }; } export async function getStaticProps({ params }) { const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`); const post = await res.json(); return { props: { post, }, }; } export default Post;
Step 3: Run the Application
-
Start the development server:
npm run dev
-
Navigate to
http://localhost:3000/blog
to see the list of blog posts. -
Click on a blog post to see the detailed view.
Summary
In this section, you learned about Static Site Generation (SSG) with Next.js. You explored key concepts such as getStaticProps
and getStaticPaths
, and saw a practical example of building a blog with SSG. By pre-rendering pages at build time, you can significantly improve the performance and SEO of your web application. Next.js makes it easy to implement SSG, allowing you to build fast and efficient static websites.
React Course
Module 1: Introduction to React
- What is React?
- Setting Up the Development Environment
- Hello World in React
- JSX: JavaScript Syntax Extension
Module 2: React Components
- Understanding Components
- Functional vs Class Components
- Props: Passing Data to Components
- State: Managing Component State
Module 3: Working with Events
Module 4: Advanced Component Concepts
- Lifting State Up
- Composition vs Inheritance
- React Lifecycle Methods
- Hooks: Introduction and Basic Usage
Module 5: React Hooks
Module 6: Routing in React
Module 7: State Management
- Introduction to State Management
- Context API
- Redux: Introduction and Setup
- Redux: Actions and Reducers
- Redux: Connecting to React
Module 8: Performance Optimization
- React Performance Optimization Techniques
- Memoization with React.memo
- useMemo and useCallback Hooks
- Code Splitting and Lazy Loading
Module 9: Testing in React
- Introduction to Testing
- Unit Testing with Jest
- Testing Components with React Testing Library
- End-to-End Testing with Cypress
Module 10: Advanced Topics
- Server-Side Rendering (SSR) with Next.js
- Static Site Generation (SSG) with Next.js
- TypeScript with React
- React Native: Building Mobile Apps