0

I would like to specify a timeout for TTFB (time to first byte).

Spec (https://xhr.spec.whatwg.org/#event-xhr-timeout) says "The author specified timeout has passed before the fetch completed.", which is not what I want.

Use case; I can possibly send media from the server, so the response can take arbitrarily long to get back.

But getting nothing back is an error condition that means I'd like to retry the request.

How?

References on timeout/ontimeout: https://stackoverflow.com/a/4958782/63621 https://fetch.spec.whatwg.org/#concept-network-error

Henrik
  • 9,714
  • 5
  • 53
  • 87

1 Answers1

0

I would like to specify a timeout for TTFB (time to first byte).

You cannot achieve this perfectly due to to the incompressible 50ms duration of the first progress event. Assuming you don't care about this imprecision, reset the timeout property or manually abort in the progress handler.

But getting nothing back is an error condition that means I'd like to retry the request.

No progress event will ever be fired if you don't receive at least one byte:

…only invoked when new bytes are transmitted.

Henrik
  • 9,714
  • 5
  • 53
  • 87
Knu
  • 14,806
  • 5
  • 56
  • 89
  • You misunderstand what I wrote, I'm not contradicting myself. I don't care about knowing the actual TTFB at runtime, only about the timeout that is possible to set on XHR to apply to the server accepting bytes. Or in other words; maybe the connection is poor; all I care about when I maybe retry (or not) is whether data is being sent regularly... (a case that collapses to TTFB) – Henrik Jul 28 '18 at 20:49
  • Then I answered your question: "just reset the timeout property … in the progress handler": i.e. use `new Date().getTime()` to calculate the elapsed time since `send` has been invoked. Aborting directly would be easier; it wouldn't fire the `ontimeout` handler though. – Knu Jul 28 '18 at 21:04
  • I edited your answer to reflect this and marked it as answered. I didn't know you could overwrite `timeout` on the client, from callbacks. Thanks for answering. – Henrik Jul 29 '18 at 18:40