question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server-side HTTP requests via `getSession` usually get extremely slow on Vercel

See original GitHub issue

Describe the bug

Before further ado, here’s a comparison between a /api/user/{id} request which fetches /api/auth/session through the network even on the server side, versus a /api/site/{id} call, which isn’t protected via getSession. Unfortunately, there’s a quite large margin between response times.

image

Steps to reproduce Call getSession on the server side and observe that it makes an HTTP request instead of just calling functions locally.

Expected behavior Calls to getSession should be executed locally in a server environment. A separate getServerSession method could be added as a non-isomorphic solution without any HTTP calls.

Additional context According to the official Next.js docs:

You should not use fetch() to call an API route in getServerSideProps. Instead, directly import the logic used inside your API route. You may need to slightly refactor your code for this approach.

Fetching from an external API is fine!

This seems to hurt performance badly.

Feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • Found the documentation helpful
  • Found documentation but was incomplete
  • Could not find relevant documentation
  • Found the example project helpful
  • Did not find the example project helpful

Thank you for maintaining this wonderful library – it really helps a lot! 🙏

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:30 (17 by maintainers)

github_iconTop GitHub Comments

7reactions
trentprynncommented, Nov 26, 2021

TLDR

  • This has resulted in a 100% (2X) speedup for basically every action in my application
  • Here’s source code for my habit tracking app which is now exclusively using getServerSession and runs on vercel serverless api functions

Here’s my write up of changing to use getServerSession solely in my application

Just as an update to other who are interested in the getServerSession method in regards to implementation and possible performance gains when running serverless on Vercel

I use NextAuth heavily in my open source daily habit tracking application which uses Next (Full SSR Frontend + API) + NextAuth + Prisma + Postgres and is hosted on Vercel. This means all of my API functions are deployed as server less functions and I do all of my auth checking in server side rendering methods for my pages.

Previously NextAuth’s use of http fetch in the getSession method severely hurt performance when run on vercel’s serverless functions – imagine the following flow and the amount of HTTP requests that were happening on Vercel’s platform where HTTP request startup time is the largest hindrance for performance

  1. User that’s already logged in navigates to habitapper.com (1)
    • http request (1)
  2. SSR for page does fetch request for session (2) -> finds a valid session -> redirects user to habitapper.com/habits page
    • http request (2)
  3. User loads habitapper.com/habits (3) -> SSR does fetch request for session (4) -> finds session -> uses internal method (non-http) to get habits for that user and passes them in props to component
    • http requests (3, 4)

With the new getServerSession this removes http requests (2) and (4) because the SSR method is directly accessing the logic to retrieve the session without having to make an http request.

Some notes about current implementation if you’re looking to do this in your application

  1. I had to use next-auth@0.0.0-pr.3222.41318883, when I tried using the getServerSession method with next-auth@4.0.0-beta.7 every call failed to fetch the session and I got errors in console – these went away when I upgraded to this experimental version and I assume will be fixed in auth@4.0.0-beta.8 when it’s released
  2. You’ll need to modify your pages/api/[...nextauth].ts file so you can access the NextAuthOptions object which you’ll need to export and use anywhere you want to use the getServerSession method
  3. Using getServerSession in a server side rendering function differs from using it in api route
7reactions
balazsorban44commented, Jun 21, 2021

@walid-mokrani looks promising, would you care to open a PR against our next branch? I’m pretty sure we could tune it a bit and make it part of the v4 release!

Read more comments on GitHub >

github_iconTop Results From Across the Web

getInitialProps - Data Fetching - Next.js
getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data...
Read more >
getserversideprops nextauth - You.com | The AI Search ...
I have have an NextJS application that uses some proxy rewrite to add auth headers from session to request header. This works fine...
Read more >
nextjs & next-auth getSession() in getServerSideProps with ...
This behaviour is normal. The values are internal to next-auth . When NEXTAUTH_URL is prefixed with https , cookies will be marked as...
Read more >
What's New in Next.js 13 - AppSignal Blog
Vercel and Next.js have been discussing this topic for some time, providing you with docs to generate OG images via functions. But now...
Read more >
Supabase Documentation
supabase-js always returns a data object (for success), and an error response (for unsuccessful requests). This provides a simple interface to get the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found