![]() |
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 :Steps to Setup Nextjs AppStep 1: Use the following command and set a Next.Js project: npx create-next-app demodisablessr ![]() 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. ![]() project structure. The updated dependencies in package.json file: "dependencies": { Steps to Disable Server Side rendering on Some Pages in Next.JsStep 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 Directive1. For a Whole PageTo 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:
By adding ‘use client’; at the top of the file, you ensure that this page will only be rendered on the client side. ![]() whole page rendered on client side only 2. For a Specific ComponentTo disable SSR for a specific component, you can also use the use client directive at the top of the component file.
3: Verify the Client-Side Rendering1. 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. ![]() Verify client only component ConclusionBy 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. |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |