Next.js Discord

Discord Forum

Website very slow, i think its my serverSideSessionHandler

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
Hi Kids!
I'm still using the pages directory and next-auth.

this is my first time building a more sophsiticated app with nextjs and i think i am doing it wrong....

All the protected pages follow this pattern:

export async function getServerSideProps(context) {
  // can put more props in passThrough object to be passed through
  return ServerSideSessionHandler(context, passThrough);
}



Some pages also use useSession, if i want to fetch data on the client side


The ServerSideSessionHandler is long and complicated and sometimes does a lot of database work. One of the key things it does is handling routing of new users and such.

I'm pretty sure this is an anti-pattern, but what is the correct pattern?

8 Replies

Cape lionOP
for more context, here's the handler

const ServerSideSessionHandler = async (
  context: GetServerSidePropsContext,
  props?: object
) => {
  const session = await getServerSession(context.req, context.res, authOptions);
  
  const currentUrl = decodeURIComponent(context.resolvedUrl);


  if (!session && currentUrl.match(`/dashboard`)) {
    return redirectTo(`/`);
  }

  if (session?.user.logins === 0) {
    console.log("welcome new user from ServerSideSessionHandler");
    return redirectTo(`/auth/new-user`);
  }

  const teamSlug = await getTeamSlugFromSession(session);

  if (currentUrl.match(/\[teamslug\]/)) {
    const newUrl = currentUrl.replace(/\[teamslug\]/, teamSlug);
    console.log(`redirecting to ${newUrl}`);
    return redirectTo(newUrl);
  }

  if (
    session &&
    teamSlug &&
    (currentUrl === "/" ||
      currentUrl.match("/auth/new-user") ||
      currentUrl === "/auth/check-email")
  ) {
    return redirectTo(`/${teamSlug}/dashboard/pages`);
  }

  if (
    session &&
    currentUrl.match(`/dashboard`) &&
    !currentUrl.match(teamSlug) &&
    !session.user?.isSuperAdmin
  ) {
    console.error(
      "ServerSideSessionHandler caught a user trying to access a team they are not a part of"
    );
    const newDirs = currentUrl.split("/").splice(2).join("/"); // remove the first directory
    return redirectTo(`/${teamSlug}/${newDirs}`);
  }

  const returnSession = JSON.parse(JSON.stringify(session));

  return {
    props: {
      session: returnSession,
      currentUrl,
      teamSlug,
      ...props,
    },
  };
};

export default ServerSideSessionHandler;
Peterbald
Hi
I have checked your issue.
I think You want to secure some urls with user's session and role.
At that time you can create middleware instead of those ServerSideSessionHandler function.
It can be more easier than ever.
https://nextjs.org/docs/app/building-your-application/routing/middleware
Cape lionOP
Thanks! Will getting the session in middleware hit the database?
Peterbald
If you are going to hit the database in middleware then you will face vercel edge error.
If you are not deploying this project on vercel there are no error. But if you select vercel to host project you will get vercel edge runtime error
This error can also be fixed
So what database are you using?
SQL or MongoDB?