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

Object.Fetch failed cause: other side closed #2910

Closed
JojokCreator opened this issue Mar 3, 2024 · 1 comment
Closed

Object.Fetch failed cause: other side closed #2910

JojokCreator opened this issue Mar 3, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@JojokCreator
Copy link

I have a static site on Vercel that makes a number of fetch requests to a Strapi backend on build. It occasionally throws this error.

    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

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>({
    endpoint,
    query,
    wrappedByKey,
    wrappedByList,
}: 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
    }
}
@JojokCreator JojokCreator added the bug Something isn't working label Mar 3, 2024
@KhafraDev
Copy link
Member

Duplicate of #1923 (and others). But if this happens when you're deploying the server probably is just forcefully closing the connection, which isn't an issue related to undici.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants