Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Next.js Tutorial
  • Next.js Components
  • Next.js Functions
  • Next.js Deployment
  • Next.js Projects
  • Next.js Routing
  • Next.js Styles
  • Next.js Server-Side Rendering
  • Next.js Environment Variables
  • Next.js Middleware
  • Next.js Typescript
  • Next.js Image Optimization
  • Next.js Data Fetching
Open In App
Next Article:
Server Side Rendering using Express.js And EJS Template Engine
Next article icon

How to Disable Server Side Rendering on Some Pages in Next.js ?

Last Updated : 01 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Next.js 13 and above versions you can use the 'use client' directive to disable the server side rendering and enable the client side rendering for some specific pages or components. This directive indicates that the code should be executed on clint side only.

Prerequisites :

  • Nodejs
  • Next.js, ReactJs and JavaScript
  • html, css

Steps to Setup Nextjs App

Step 1: Use the following command and set a Next.Js project:

npx create-next-app demodisablessr
cd demodisablessr
pse
select the option with left right arr and hit enter

Step 2: Create a components folder in pages directory add a csr.js file and make sure the structure is as follows.

ps
project structure.

The updated dependencies in package.json file:

"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.4"
}

Steps to Disable Server Side rendering on Some Pages in Next.Js

Step 1: Update to Next.js 13+

Ensure your Next.js project is updated to version 13 or later, as the use client directive is introduced in this version.

npm install next@latest

Step 2: Using the use client Directive

1. For a Whole Page

To disable SSR for an entire page, add the use client directive at the top of the page component file.

Add the use client directive at the top of the file:

JavaScript
// pages/index.js "use client";  import { useState } from "react";  const ClientOnlyPage = () => {     const [counter, setCounter] = useState(0);      return (         <div>             <h1>Client-Only Page</h1>             <p>Counter: {counter}</p>             <button                 onClick={() =>                     setCounter(counter + 1)                 }             >                 Increment Counter             </button>         </div>     ); };  export default ClientOnlyPage; 

By adding 'use client'; at the top of the file, you ensure that this page will only be rendered on the client side.

csrp
whole page rendered on client side only

2. For a Specific Component

To disable SSR for a specific component, you can also use the use client directive at the top of the component file.

JavaScript
// components/ClientOnlyComponent.js "use client";  import { useEffect, useState } from "react";  const ClientOnlyComponent = () => {     const [data, setData] = useState(null);      useEffect(() => {         const fetchData = async () => {             const response = await fetch(                 "/api/data"             );             const result = await response.json();             setData(result);         };          fetchData();     }, []);      return (         <div>             <h2>Client-Only Component</h2>             <pre>                 {JSON.stringify(data, null, 2)}             </pre>         </div>     ); };  export default ClientOnlyComponent; 
JavaScript
// pages/index.js import ClientOnlyComponent from "../components/ClientOnlyComponent";  const HomePage = () => {     return (         <div>             <h1>Home Page</h1>             <ClientOnlyComponent />         </div>     ); };  export default HomePage; 

3: Verify the Client-Side Rendering

1. Run Next.js application with following command.

npm run dev

2. Inspect the Page Source: Right-click on the page and select "View Page Source". You should see the minimal HTML content for the ClientOnlyComponent, indicating that it is not pre-rendered on the server and is rendered on the client side.

3. Check the Functionality: Click the "Increment Counter" button and observe the counter incrementing, confirming that the component is functioning as expected with client-side rendering.

csrr
Verify client only component

Conclusion

By using the use client directive at the top of your component or page files in Next.js 13 and later, you can easily disable server-side rendering for specific parts of your application. or if you want to disable at application level or root level of pages one can use directive in pages index.js file as shown in this article.


Next Article
Server Side Rendering using Express.js And EJS Template Engine

F

firozsh553l
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Next.js
  • Next.js - Questions

Similar Reads

  • Node.js Server Side Rendering (SSR) using EJS
    Server-side rendering (SSR) is a popular technique for rendering a normally client-side-only single-page app (SPA) on the server and then sending a fully rendered page to the client. The client's JavaScript bundle can then take over and the SPA can operate as normal. SSR technique is helpful in situ
    3 min read
  • Server-Side Rendering (SSR) with React Hooks
    Server-side rendering (SSR) is a technique used to render web pages on the server side and send the fully rendered page to the client's browser. This approach offers benefits such as improved SEO and faster initial page loads. With the introduction of React Hooks, SSR implementation has become even
    4 min read
  • Server Side Rendering using Express.js And EJS Template Engine
    Server-side rendering involves generating HTML on the server and sending it to the client, as opposed to generating it on the client side using JavaScript. This improves initial load time, and SEO, and enables dynamic content generation. Express is a popular web application framework for NodeJS, and
    3 min read
  • Next.js Authenticating Server-Rendered Pages
    In Next.js, server-side authentication for pages ensures that user data and access permissions are validated before rendering pages on the server. This approach enhances security and provides a better user experience by ensuring that only authorized users can access specific pages. In this article,
    6 min read
  • How to Change URL Without Page Refresh in Next.js?
    In web development, the ability to change URLs dynamically without triggering a full page refresh is essential for creating smooth, seamless user experiences. Next.js, a popular React framework, offers built-in features to achieve this efficiently through its client-side routing capabilities. In thi
    3 min read
  • Importance of View Engines in server-side rendering(SSR)
    A view engine is a tool used in web development to create dynamic HTML content based on data from the server. It acts as a template processor that allows you to integrate data with predefined HTML templates easily. In this article, we will learn about the Importance of view engines on server side re
    3 min read
  • How to use Routes with serve-static Files in Node.js ?
    Using serve-static files with routes in a Node.js application involves creating a server with Express, defining routes, and serving static files like HTML, CSS, JavaScript, images, etc. Here’s an article explaining how to use serve-static with routes in Node.js. Serving Static Files with Routes in N
    3 min read
  • How to Share Data Between Pages in Next.js?
    Sharing data between pages in Next.js can be crucial for maintaining state and passing information efficiently. You can achieve this with the help of URL parameters. In this article, we will learn about how to share data between pages in next.js Approach to Share Data Between PagesUse the Link compo
    2 min read
  • How to Handle a Post Request in Next.js?
    NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to handle a post request in NextJS. A
    2 min read
  • Server-Side Rendering in Angular
    With fast and efficient web applications, developers are continually seeking ways to enhance performance, user experience, and search engine optimization (SEO). One such strategy is server-side rendering (SSR). In this article, we will see what SSR is, why it is important, and how to implement it in
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences