Error when building static site from Strapi

Hi guys, I’m new to strapi but keep getting a

    at async file:///vercel/path0/dist/chunks/prerender_z0pSUssE.mjs:614:21
    at async Object.render (file:///vercel/path0/dist/chunks/astro_4VOd5ehm.mjs:1572:7)
    at async Object.renderToFinalDestination (file:///vercel/path0/dist/chunks/astro_4VOd5ehm.mjs:866:7)
    at async renderChild (file:///vercel/path0/dist/chunks/astro_4VOd5ehm.mjs:1059:5)
  Caused by:
  other side closed
    at TLSSocket.onSocketEnd (node:internal/deps/undici/undici:7998:26)
    at endReadableNT (node:internal/streams/readable:1368:12)
Error: Command "npm run build" exited with 1

Error when building my site. Strapi doesn’t seem to throw any errors. Trying to work out whether this is a Strapi or a Vercel error.

It only happens occasionally and if I reploy a few times it works eventually, so I’m guessing it’s not a code error.

The fetch code is.

interface Props {
    endpoint: string;
    query?: string;
    wrappedByKey?: string;
    wrappedByList?: boolean;

 * Fetches data from the Strapi API
 * @param endpoint - The endpoint to fetch from
 * @param query - The query parameters to add to the URL
 * @param wrappedByKey - The key to unwrap the response from
 * @param wrappedByList - If the response is a list, unwrap it
 * @returns
export default async function fetchApi<T>({
}: Props): Promise<T> {
    try {
        if (endpoint.startsWith('/')) {
            endpoint = endpoint.slice(1);

        const url = new URL(`${import.meta.env.STRAPI_URL}/api/${endpoint}`);
        const queryString = query ? `?${query}` : '';
        const urlWithQuery = `${url.toString()}${queryString}`;
        const res = await fetch(urlWithQuery, {
            method: "GET",
            headers: {
                Authorization: `Bearer ${import.meta.env.STRAPI_KEY}`,
                "Content-Type": "application/json",
        if (!res.ok) {
            throw new Error(`HTTP error! Status: ${res.status}`);

        let data = await res.json();
        if (wrappedByKey) {
            data = data[wrappedByKey];

        if (wrappedByList) {
            data = data[0];

        return data as T;
    } catch (error) {
        // Handle errors here
        console.error("Error fetching data:", error);
        throw error; // Rethrow the error