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

Provide a Timeout Feature to EleventyFetch #39

Closed
cromeoli opened this issue Jan 11, 2024 · 1 comment
Closed

Provide a Timeout Feature to EleventyFetch #39

cromeoli opened this issue Jan 11, 2024 · 1 comment

Comments

@cromeoli
Copy link

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.

@cromeoli cromeoli added the enhancement New feature or request label Jan 11, 2024
@zachleat zachleat transferred this issue from 11ty/eleventy Jan 18, 2024
@zachleat
Copy link
Member

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)
	},
});

@zachleat zachleat added this to the Eleventy Fetch v5.0.0 milestone May 13, 2024
@zachleat zachleat removed the enhancement New feature or request label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants