vercel Serverless Function has timed out

I’m working on a Next.js project where I need to fetch markdown files from a GitHub repository using an API route. The API route fetches and processes the markdown files, then returns the data to be used in a blog page. The setup works fine locally, but when deploying to Vercel, I’m facing performance issues and timeouts, likely due to Vercel’s Hobby plan limitations.

What I tried:

API Route for Fetching Blog Data:
I created an API route in Next.js to fetch markdown files from a GitHub repository. This route uses axios to fetch the file content, gray-matter to parse the front matter, and then returns the parsed data.

import axios from "axios";
import matter from "gray-matter";
import type { NextApiRequest, NextApiResponse } from "next";

const OWNER_NAME = "Attraxia";
const REPO_NAME = "Blogs";
const TOKEN = process.env.NEXT_PUBLIC_GITHUB_BLOGS_REPO_ACCESS_TOKEN!;

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    const { slug } = req.query;

    if (!slug || Array.isArray(slug)) {
        return res.status(400).json({ error: "Invalid or missing slug parameter" });
    }

    try {
        const url = `https://api.github.com/repos/${OWNER_NAME}/${REPO_NAME}/contents/Blogs/${slug}.mdx?ref=dev`;

        const response = await axios.get(url, {
            headers: {
                Authorization: `token ${TOKEN}`,
            },
        });

        const content = Buffer.from(response.data.content, "base64").toString("utf-8");
        const { data: metadata, content: mdxContent } = matter(content);

        return res.status(200).json({ metadata, content: mdxContent });
    } catch (error) {
        return res.status(500).json({ error: "Failed to fetch blog data" });
    }
}

Using getServerSideProps for SEO:
I called this API route within getServerSideProps to fetch the blog data server-side for SEO purposes.

import React from "react";
import axios from "axios";
import { BlogProvider } from "context/blogs/BlogContext";
import { BlogData } from "context/blogs/BlogContext/BlogContext.types";
import { GetServerSideProps } from "next";

import WrappedBlogPage from "@features/blogs/Blog/Blog";

interface BlogProps {
    initialBlogData: BlogData | null;
}

const Blog: React.FC<BlogProps> = ({ initialBlogData }) => {
    return (
        <BlogProvider initialBlogData={initialBlogData}>
            <WrappedBlogPage />
        </BlogProvider>
    );
};

export const getServerSideProps: GetServerSideProps = async (context) => {
    const { slug } = context.query;
    const { req } = context;
    const baseURL = req ? `http://${req.headers.host}` : "";

    try {
        const response = await axios.get(`${baseURL}/api/blog/fetchBlogData?slug=${slug}`);

        return {
            props: {
                initialBlogData: response.data,
            },
        };
    } catch (error) {
        return {
            props: {
                initialBlogData: null,
            },
        };
    }
};

export default Blog;

Context Setup for Blog Data:
I set up a context to manage the blog data state.

import React, { createContext, ReactNode, useContext, useEffect, useMemo } from "react";
import { MDXProvider } from "@mdx-js/react";

import components from "@features/blogs/Blog/components/MDX/MDXComponents";

import { BlogContextType, BlogData } from "./BlogContext.types";

const BlogContext = createContext<BlogContextType | undefined>(undefined);

export const BlogProvider: React.FC<{ children: ReactNode; initialBlogData: BlogData | null }> = ({
    initialBlogData,
    children,
}) => {
    const [loading, setLoading] = React.useState<boolean>(true);
    const [isError, setIsError] = React.useState<boolean>(false);
    useEffect(() => {
        if (!initialBlogData) {
            setIsError(true);
        } else {
            setLoading(false);
        }
    }, [initialBlogData]);

    const ContextValues = useMemo(() => ({ initialBlogData, loading, isError }), [initialBlogData, loading, isError]);

    return (
        <BlogContext.Provider value={ContextValues}>
            <MDXProvider components={components}>{children}</MDXProvider>
        </BlogContext.Provider>
    );
};

export const useBlog = (): BlogContextType => {
    const context = useContext(BlogContext);
    if (!context) {
        throw new Error("useBlog must be used within a BlogProvider");
    }
    return context;
};

What I Expected:

I expected the API route to quickly fetch the blog data from the GitHub repository and return it to the getServerSideProps function. The blog data should then be passed to the page component via props and rendered correctly. This setup should ensure good SEO performance since the blog content is rendered server-side.

However, I’m encountering performance issues and timeouts on Vercel, especially given the constraints of the Hobby plan. The API route is often slow, leading to delays and occasional failures in fetching the blog data.

Any insights on how to optimize the performance of this API route would be greatly appreciated!

New contributor

Zied Abadlia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật