Search code examples
vercelsveltekit

SvelteKit Vercel deployment - Serverless Function Timeout Error


I'm encountering a FUNCTION_INVOCATION_TIMEOUT error when deploying my SvelteKit app with server-side rendering (SSR) on Vercel. The deployment fails with a 504: GATEWAY_TIMEOUT error, indicating that a serverless function has timed out after 10 seconds.

My SvelteKit app includes a serverless function to fetch data from a MongoDB database and render it on the client side. However, the serverless function appears to exceed the timeout limit during deployment.

I've optimized my serverless function to reuse MongoDB connections, close connections after use, and added error handling to catch exceptions. Despite these optimizations, the deployment still fails with a timeout error.

Here's a simplified version of my serverless function (+server.ts):

import { Collection, MongoClient, type Document } from "mongodb";
import { json } from "@sveltejs/kit";

let cachedDb: { client: MongoClient; blogCollection: Collection<Document>; } | null = null;

async function connectToDatabase() {
  if (cachedDb) {
    return cachedDb;
  }

  const client = new MongoClient(DB_URI);
  await client.connect();
  const db = client.db("blogs");
  const blogCollection = db.collection("blogs");

  cachedDb = { client, blogCollection };
  return cachedDb;
}

export async function GET() {
  try {
    const { blogCollection } = await connectToDatabase();
    const result = await blogCollection.find().toArray();
    return json(result, { status: 201 });
  } catch (error) {
    console.error("GET request error:", error);
    return json({ error: "Internal Server Error" }, { status: 500 });
  }
}

And +page.ts for SSR

export type PageServerLoad = (props: {
  fetch: typeof fetch;
}) => Promise<{ [key: string]: any }>;

let blogs = [];

export const load: PageServerLoad = async ({ fetch }) => {
  const response = await fetch("/api/blogs");
  blogs = await response.json();

  return {
    blogs,
  };
};

Here is screenshot of error page enter image description here

NOTE
. Deployment is successfull when CSR is used.
. I'm on a free version on Vercel


Solution

  • After struggling couple of hours I found out that there was a restriction accessing ip address in mongodb atlas.
    It's weird, that no error text was highlighting that.

    Here are steps to fix:

    In MongoDB Atlas, by default, connections are only allowed from specific IP addresses for security reasons. This means that if you want to connect to your MongoDB Atlas cluster from your local machine or any other specific IP address, you need to whitelist that IP address in the MongoDB Atlas dashboard. Here's how you can do it:

    • Log in to your MongoDB Atlas account and navigate to your project.
    • Go to the "Network Access" tab in the left sidebar menu.
    • Click on the "Add IP Address" button.
    • Allow access from everywhere or specify a one you need

    After adding the IP address, make sure to save the changes.

    After redeploy deployment was successful.