Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EDGE_FUNCTION_INVOCATION_TIMEOUT when using DrizzleAdapter | timeout Vercel edge functions #10773

Open
tobiasmeyhoefer opened this issue Apr 30, 2024 · 10 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@tobiasmeyhoefer
Copy link

tobiasmeyhoefer commented Apr 30, 2024

Environment

Next Auth v5, Vercel, Neon, Drizzle

Reproduction URL

https://github.com/tobiasmeyhoefer/bitz

Describe the issue

In localhost everything works fine but when deployed with Vercel hitting this error every second or third request when the middleware gets called...

long waiting and then this example error message:
[GET] [middleware: "middleware"] /browse reason=EDGE_FUNCTION_INVOCATION_TIMEOUT, status=504, user_error=true

I already tried different package versions and setting location near me

How to reproduce

you can clone and try to deploy it yourself or look here: https://bitz-ecru.vercel.app

Im using neon serverless database... one thing that is sus to me is that the file db.ts:

gets called every time, shouldn't the connection be established only once?

Expected behavior

no errors at all

@tobiasmeyhoefer tobiasmeyhoefer added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Apr 30, 2024
@wrod7
Copy link

wrod7 commented May 9, 2024

having the same issue using prisma neon adaptor

@Yvon-Data
Copy link

having the same issue with planetscale adapter

@kisankumavat85
Copy link

I am having the same issue, I am using App Router (v14), Prisma, Neon and Next-Auth v5.

@Muhammad-RK-Isa
Copy link

I'm having the same issue while implementing Role Based authentication using Next-auth v5. Everything works fine in development but edge function get timed out in production.

Note: It's not a Vercel issue as I've tried Netlify too and got the same result. Having this issue for two weeks now.

Next-auth v4 doesn't have this kinda issues. But as I'm want to implement auth using v5, I've tried multiple ways of fixing this issue and nothing seems to be working :)

@ndom91
Copy link
Member

ndom91 commented May 19, 2024

Depending on your setup, you may be running into issues where your adapter is not "edge compatible" and is timing out trying to start it up, etc. Please check out our edge compatibility guide.

If you've confirmed your adapter is "edge compatible", then you may also be doing too many DB actions in your middleware. The limit there seems to be "25s to begin returning data".

That's an awfully long time though, so unless yuo're doing something extra heavy you shouldn't be hitting that limit without other issues I'd guess.

Please provide some more details, like versions of all the relevant packages and some log output and we can try and figure out what's going on 🙏

@Yvon-Data
Copy link

This is the only log I have in production:
[GET] [middleware: "middleware"] /dashboard/company reason=EDGE_FUNCTION_INVOCATION_TIMEOUT, status=504, user_error=true

Versions:

"@auth/prisma-adapter": "^2.0.0",
"next-auth": "5.0.0-beta.17",'

Here is my db instance:

import { Client } from "@planetscale/database"
import { PrismaPlanetScale } from "@prisma/adapter-planetscale"
import { PrismaClient } from "@prisma/client"

import { env } from "@/env.mjs"

const connectionString = `${env.DATABASE_URL}`

const client = new Client({ url: connectionString })
const adapter = new PrismaPlanetScale(client)
const prisma = new PrismaClient({ adapter })

export const db = prisma

My auth.ts:

import { PrismaAdapter } from "@auth/prisma-adapter"
import NextAuth from "next-auth"
import type { Provider } from "next-auth/providers"
import GitHub from "next-auth/providers/github"
import Google from "next-auth/providers/google"

import { db } from "@/lib/db"

const providers: Provider[] = [GitHub, Google]

export const config = {
  adapter: PrismaAdapter(db),
  providers: providers,
  pages: {
    signIn: "/login",
  },
  callbacks: {
    async session({ token, session }) {
      if (token) {
        session.user.id = token.id
        session.user.name = token.name
        session.user.email = token.email
        session.user.image = token.picture
      }

      return session
    },
    async jwt({ token, user }) {
      const dbUser = await db.user.findFirst({
        where: {
          email: token.email,
        },
      })

      if (!dbUser) {
        if (user) {
          token.id = user?.id
        }
        return token
      }

      return {
        id: dbUser.id,
        name: dbUser.name,
        email: dbUser.email,
        picture: dbUser.image,
      }
    },
  },
}

export const { handlers, auth, signIn, signOut } = NextAuth(config)

And my middleware:

import { NextResponse } from "next/server"
import { auth } from "@/auth"
import {
  DEFUALT_LOGIN_REDIRECT,
  apiAuthPrefix,
  authRoutes,
  publicRoutes,
} from "@/routes/auth"

export default auth((req) => {
  const { nextUrl } = req
  const isLoggedIn = !!req.auth

  const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix)
  const isPublicRoute = publicRoutes.includes(nextUrl.pathname)
  const isAuthRoute = authRoutes.includes(nextUrl.pathname)

  if (isApiAuthRoute) {
    return
  }

  if (isAuthRoute) {
    if (isLoggedIn) {
      return NextResponse.redirect(new URL(DEFUALT_LOGIN_REDIRECT, nextUrl))
    }
    return
  }

  if (!isLoggedIn && !isPublicRoute) {
    return NextResponse.redirect(new URL("/login", nextUrl))
  }

  return
})

export const config = {
  matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
}

@ndom91
Copy link
Member

ndom91 commented May 20, 2024

@Yvon-Data can you share the versions of all the relevant prisma packages as well?

@Yvon-Data
Copy link

Yvon-Data commented May 21, 2024

Of course!

"prisma": "^5.13.0",
"@prisma/adapter-planetscale": "^5.13.0",
"@prisma/client": "^5.13.0"

@ndom91
Copy link
Member

ndom91 commented May 22, 2024

Of course!

"prisma": "^5.13.0",
"@prisma/adapter-planetscale": "^5.13.0",
"@prisma/client": "^5.13.0"

Hmm okay so it looks like that should be "edge compatible", but your middleware / auth.js setup is relatively simple so I can't imagine its anything else other than the DB lookup in your jwt callback 🤔

Might be worthwhile testing another DB call in an unrelated edge api route, like shown in this prisma + planetscale doc: https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-vercel#planetscale, to see if you run into similar errors there or if that works as expected

@kisankumavat85
Copy link

I downgraded next-auth to v4 and now its working like a charm. I was also using edge compatible prisma but the issue is with next-auth v5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

6 participants