Skip to content

Commit

Permalink
Update nextvs post (#6414)
Browse files Browse the repository at this point in the history
  • Loading branch information
necatiozmen authored Oct 21, 2024
1 parent 32c4549 commit 0d2d2f5
Showing 1 changed file with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ description: We briefly run through the innovation driven differences between Re
slug: next-js-vs-react
authors: marvel_ken
tags: [react, nextjs]
image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-10-10-nextjs-vs-react/social.png
image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-10-10-nextjs-vs-react/social-2.png
hide_table_of_contents: false
---

**_This article was last updated on February 2, 2024 to organize content and add more use cases for better understanding of Next.js vs React comparison._**
**This article was last updated on October 17, 2024 to include more details on SEO considerations, image handling, security features, and to provide additional use cases for a better understanding of the Next.js vs React comparison.**

## Introduction

Expand All @@ -23,33 +23,20 @@ In this post, we provide an in-depth account of how Next.js compares to React -
Steps we'll cover:

- [What is React?](#what-is-react)
- [What is Next.js?](#what-is-nextjs-)
- [What is Next.js?](#what-is-nextjs)
- [Next.js vs React.js: The Core Differences](#nextjs-vs-reactjs-the-core-differences)
- [Server Side Rendering vs Client Side Rendering](#server-side-rendering-vs-client-side-rendering)
- [What is Server-Side Rendering?](#what-is-server-side-rendering-)
- [What is Client Side Rendering?](#what-is-client-side-rendering-)
- [Advantages of Client Side Rendering](#advantages-of-client-side-rendering)
- [CSR or SSR: Which Pattern is Better?](#csr-or-ssr-which-pattern-is-better-)
- [Static Site Generation](#static-site-generation)
- [React: The Foundation](#react-the-foundation)
- [Next.js: Enhanced SSG Capabilities](#nextjs-enhanced-ssg-capabilities)
- [Data Fetching and Caching](#data-fetching-and-caching)
- [React: Data Fetching and Caching with External Libraries](#react-data-fetching-and-caching-with-external-libraries)
- [Data Fetching in Next.js](#data-fetching-in-nextjs)
- [Routing](#routing)
- [React Routing with Library of Your Choice](#react-routing-with-library-of-your-choice)
- [Next.js: File Based Opinionated Routing](#nextjs-file-based-opinionated-routing)
- [Next.js vs React: SEO Considerations](#nextjs-vs-react-seo-considerations)
- [Handling Images in React vs Next.js](#handling-images-in-react-vs-nextjs)
- [Security Features in Next.js vs React](#security-features-in-nextjs-vs-react)
- [Middleware for Protecting Routes](#middleware-for-protecting-routes)
- [React vs Next.js: Backend API Development](#react-vs-nextjs-backend-api-development)
- [Next.js vs React: Configuration Differences](#nextjs-vs-react-configuration-differences)
- [React Configuration](#react-configuration)
- [Next.js Configuration](#nextjs-configuration)
- [React vs Next.js: Use Cases and Suitability](#react-vs-nextjs-use-cases-and-suitability)
- [When to Use Next.js](#when-to-use-nextjs)
- [When to Use React](#when-to-use-react)
- [React and Next.js: Starting Up Both Projects](#react-and-nextjs-starting-up-both-projects)
- [Starting a React App](#starting-a-react-app)
- [How to Initialize a React App](#how-to-initialize-a-react-app)
- [Initializing a Next.js App](#initializing-a-nextjs-app)

## What is React?

Expand Down Expand Up @@ -376,6 +363,83 @@ Again, this is more opinionated than plain React Router `<Link />` components.

Conventions like file based routing have potential lock-in drawbacks but Next.js can lead our way long before we decide on some custom configuration, opt for other conventions or think of stepping back and start building ground up with pure React. But Next.js gives mostly what an average React developer would want to already have if they are given a choice to build routing from scratch.

## Next.js vs React: SEO Considerations

React can be challenging for SEO because it relies on client-side rendering, meaning content is generated in the browser. Search engines might not always properly index content that is dynamically loaded.

On the other hand, Next.js supports server-side rendering (SSR) and static site generation (SSG), which pre-renders content on the server. This makes it easier for search engines like Google to index your pages, leading to better SEO. If SEO is important, Next.js is likely the better choice.

## Handling Images in React vs Next.js

In React, handling images can require manual work. You need to optimize them yourself or use third-party tools for things like lazy loading or resizing.

Next.js simplifies this with the built-in <Image /> component, which automatically optimizes images, including lazy loading and resizing for different devices. Here’s an example:

```tsx
import Image from "next/image";

const MyComponent = () => (
<Image
src="/path/to/image.jpg"
alt="Description of image"
width={500}
height={500}
/>
);
```

Next.js handles the rest, making it a great option for image-heavy websites.

## Security Features in Next.js vs React

React is just a frontend library, so for security, you’ll need to manage things like authentication and authorization with a backend or third-party service. It doesn’t include built-in security features.

Next.js, however, is a full-stack framework. You can build backend APIs and manage security directly in your app. For instance, you can use API routes for authentication and protect sensitive data. Middleware in Next.js can also help check authentication before rendering a page.

### API Route for Authentication

```tsx
// pages/api/secure-data.js

export default function handler(req, res) {
const { token } = req.headers;

if (token === "your-secret-token") {
res.status(200).json({ message: "You have access to secure data!" });
} else {
res.status(401).json({ error: "Unauthorized access" });
}
}
```

This API route checks if a valid token is passed in the request headers. If the token matches, access is granted; otherwise, it returns a 401 error.

## Middleware for Protecting Routes

You can also use middleware in Next.js to protect routes by checking user authentication:

```tsx
// pages/api/secure-data.js

export default function handler(req, res) {
const { token } = req.headers;

if (token === "your-secret-token") {
res.status(200).json({ message: "You have access to secure data!" });
} else {
res.status(401).json({ error: "Unauthorized access" });
}
}
```

In this example:

• It checks for an authentication token in the cookies.
• If the token doesn’t exist, the user is redirected to the login page.
• If the token exists, the request proceeds normally.

To apply middleware to specific routes, you would add it to the middleware.js file and configure the routes accordingly.

**In Short:**

- **Simplicity vs. Flexibility**: Next.js offers a straightforward, file-based routing system, making it easy for developers to set up routes without additional configuration. React, provides more flexibility but requires manual setup of a lirbary of choice.
Expand Down

0 comments on commit 0d2d2f5

Please sign in to comment.