A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/nextjs/next-js-getserversideprops-function/ below:

Next.js getServerSideProps() Function - GeeksforGeeks

Next.js getServerSideProps() Function

Last Updated : 23 Jul, 2025

The getServerSideProps method is used to fetch data and pre-render the page. It enables server-side rendering. As a result, it's particularly useful for sites with dynamic data or requests that must be made often, such as retrieving authorized user data.

Syntax:
export async function getServerSideProps(context) {
return {
props: {},
}
}
Parameter:

It accepts the context object which can have the following properties:

Returns:

An object containing any of the following properties is returned by the getServerSideProps function:

Steps to SetUp Next Project

To run the examples given ahead, you will need to create a Next.js project. For the purposes of demonstration, I have created a project named test-project. You can do so by typing the following command:

Step 1: Create a project folder and move into it by using the below command in the terminal:

mkdir foldername
cd foldername

Step 2: In that foldername, create your project by using the below command in the terminal:

npx create-next-app test-project

Step 3: Create two additional files namely about.js and home.js in the pages directory for the examples

Project Structure:

This project should look like this:

 

Steps to run the application: To run the server in development mode, you will need to type the following command:

npm run dev

Note: By default, it will start the server on port 3000, however, if the port is already in use it will choose the first port which is free starting from 3001.

Example 1: Type the following code in pages/about.js. You can visit localhost:3000/about to view the output.

JavaScript
// Filename - about.js

export default function About({ message }) {
    return (
        <div>
            <h1>{message}</h1>
        </div>
    );
}
  
export function getServerSideProps() {
    return {
        props: { message: "Welcome to the About Page" },
    };
}

Output:

Example 2: Type the following code in pages/home.js. You can visit localhost:3000/home to view the output.

JavaScript
// Filename - home.js

export default function Home({ message }) {
    return (
        <div>
            <h1>This is Home Page</h1>
            <h2>{message}</h2>
        </div>
    );
}
  
export function getServerSideProps() {
    return {
        notFound: true
    };
}

Output:

Reference: https://nextjs.org/docs/pages/api-reference/functions/get-server-side-props



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4