skip to Main Content

So, my instructor building an application, he told me to use getServerSideProps(context).
Since Next.js updated, I want to know what context means and what does it holds? Since the course was outdated, can any please tell me the alternative method? here’s my instructor code:
userPage.jsx:

import { getSession } from "next-auth/react";
import React from "react";

const UserPage = () => {
  return <div>User</div>;
};

export default UserPage;
export async function getServerSideProps(context) {
  const session = await getSession(context);
  console.log("sess", session);
  return {
    props: {},
  };
}

I tried and searched a lot for this and iam tired for this. Please Ignore This Question if you think this is Stupid 🙂

2

Answers


  1. Context provides information about the request, including matched params for dynamic routes, query parameters, request cookies, and more.

    You can find more information about the context parameter in the official documentation.

    If you’d like to upgrade to use the /app directory pattern, you can follow this migration guide. In short, you’ll use React Server Components to directly fetch the data you need for your component.

    Login or Signup to reply.
  2. The context parameter is a special object that holds various pieces of information related to the current HTTP request being processed. Note that context object in each server-side functions (getStaticProps..) is different because their environment is different. getServerSideProps runs with every requst.

    If you look at the docs, each key is related to the HTTP request.

    req and res objects are similar in express. Here you do not need to send anything with res.send like in express.js because Next.js will do it automatically (its response is the rendered component). But you can intervene in and manipulate the res object by adding extra headers or cookies.

    with req you can intervene in incoming request and read the incoming data, headers,cookies etc. so you can check if the user authenticated or not

    params is useful to read the dynamic part of the page. In dynamic pages, param usually refers to id of stored data so you fetch the data on the server by using the id

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search