11ty/eleventy-fetch

Provide a Timeout Feature to EleventyFetch

cromeoli opened this issue · 1 comments

Is your feature request related to a problem? Please describe.

I was encountering an issue while trying to set up my 11ty project. When I attempted to run npm start, it takes an extended amount of time to initiate the dev environment, approximately 5 minutes. Upon inspecting the console, I observe a few 'ETIMEDOUT' errors. This is ok, is due to my API being currently offline and expected to remain so for a while.

So my problem was this huge wait time.

Describe the solution you'd like

I would like to be able to provide a timeout for EleventyFetch and maybe even being able to set one on a global level to reduce the build time in this scenarios.

Describe alternatives you've considered

I wrapped my EleventyFetch calls with a Promise.race like this:

const timeoutPromise = new Promise((_, reject) => {
    setTimeout(() => {
      reject(new Error("Timeout: API took to long"));
    }, 2500);
  });
apiCall = await Promise.race([apiCall, timeoutPromise]);

Additional context

I posted this as a problem into the 11ty discord server (Title: EleventyFetch problem, very long await for API response) and a few users told me that it would be good to have something like this built into EleventyFetch, and proposed me to open a feature request, so here I am. Also I reviewed the others feature request to see if there was already one or similar, but I didn't spotted it. I am writing this into 11ty-Project and not 11ty-Fetch because this feature form was formatted to receive feature requests and the 11ty-fetch form was not, so it seemed more appropriate to write it here.

If something like this already existed and I have not been able to see it, I am sorry for wasting your time, I tried to look for it and I only found this which was not exactly the same.

Thank you so much for the attention and for 11ty.

I believe this is fixed by AbortSignal and the swap to use Node-native fetch in v5.0.

https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal#aborting_a_fetch_operation_with_a_timeout

Pass in a signal to Eleventy’s fetchOptions.

EleventyFetch("https://example.com/", {
	fetchOptions: {
		signal: AbortSignal.timeout(5000)
	},
});