Recieving 502 Task timed out after 10.02 seconds from /api

I have deployed my Next.js app to Netlify. When opening the api call made on render /api return

{"errorMessage":"2023-09-21T13:45:28.121Z fa30bf71-577f-47e4-bb8d-eb7eaa93dbcb Task timed out after 10.02 seconds"}

Also in development the api is taking around 1.5/2seconds to return data so nowhere near 10s
When running in development this calls and responds with expected data.

This is my route.ts file connecting to db and making db call

export async function GET() {
  try {
    // Connect to db
    const db = await connectToDb();
    console.log("Successfully connected to Atlas");

    // Collection reference (pubs)
    const collection = await db.collection("pubs");

    // Get all documents
    const pubs = collection.find();
    const json = await pubs.toArray();
    return NextResponse.json({status: 200, data: json});
  } catch (error) {
    console.log("error", error);
    return NextResponse.json({
      code: 500,
      message: "Something went wrong.",
    });
  }
}


This i smy frontend useEffect call

useEffect(() => {
if (allPosts.length > 0) return undefined
const init = async () => {
const posts = await getData()
console.log(“posts page.tsx”, posts);
if (posts.status === 200) {
setAllPosts(posts.data)
return setFilteredPosts(posts.data)
}
}
init()
}, [allPosts]);

And this is the getData function

export default async function getData() {
  const res = await fetch("/api", {
    next: {tags: ["posts"]},
  });
  const posts = await res.json();
  console.log("getData", posts);
  return posts;
}


Cannot figure out what is failing for it to return a 502

Have you checked: [Support Guide] Why is my function taking long or timing out?