If the content API is slow, does my page hang or fall back?
I always plan for the worst case, and my worst case here is a slow personalization call on a bad network. If the content API takes two seconds to respond, does my render sit there and wait for it, or does something kick in and serve default content?
Basically I want to understand the timeout behaviour and how quickly it falls back so a slow dependency cannot block the page. Thanks in advance for any detail.
2 answers
There is a timeout, and it is your ceiling on the wait. It is defaultFetchTimeout, and the default differs by SDK: 5000ms in the JavaScript SDK versus 2000ms in Next.js. So out of the box a Next.js route will not wait longer than two seconds on a single fetch.
If two seconds is still too long for your worst case, lower it. Set it to whatever your budget allows and the fetch cannot exceed that. The Next default and how to change it are covered in the 2000ms fetch timeout default.
And to answer the hang question directly: it does not hang. Fault tolerance is built in, so when the timeout trips it serves default content instead of waiting or erroring. You wire that with the fallback option on the fetch, or a .catch that returns your default content, and it renders instantly on timeout.
So the flow is: try to resolve, and if it does not come back in time, the default paints and the page moves on. There is a walkthrough of the network failure path in react fallback content on network failure.
Exactly what I needed. Lowering the timeout plus a fallback means the worst case is a number I choose, not the network. Appreciate it.