Nuxt useFetch and Server‑Side Rendering: Interaction of Automatic Abort Signals and Timeout Handling
0 reputation · 06 Feb 2020, 10:53 UTC
0 reputation · 06 Feb 2020, 10:53 UTC
Goal: Ensure that data fetching in Nuxt 3 handles both navigation‑initiated cancellation and request timeouts uniformly, especially during server‑side rendering where no client‑side AbortSignal is available.
Currently, useFetch automatically forwards an AbortSignal when the navigating component is destroyed, but it does not apply a default timeout; during SSR the abort signal is absent, so timeout handling depends solely on the ofetch timeout option or an explicit $fetch config. This split leaves developers to create their own wrapper or rely on third‑party plugins to combine abort‑signal logic with a reusable timeout setting.
Should Nuxt expose a composable that merges abort‑signal handling with a configurable default timeout? What default value makes sense and how can it be overridden per request? How should the composable behave when running on the server where no AbortSignal exists?
35275 reputation · 06 Feb 2020, 14:05 UTC
Yes – a thin wrapper composable (e.g., useFetchWithTimeout) that automatically forwards the router‑provided AbortSignal when it exists and applies a configurable default timeout would give developers a single, uniform way to handle both navigation‑initiated cancellation and request timeouts, including during SSR where no client‑side AbortSignal is present.
useFetch forwards an AbortSignal from the router when the navigating component is destroyed, but it does not set a default request timeout.$fetch or ofetch timeout option.The split between automatic abort‑signal forwarding and manual timeout configuration forces developers to create their own wrapper or rely on third‑party plugins to combine the two concerns. A composable that merges them would eliminate that duplication.
useFetch plus an optional timeout (in milliseconds).useRouter() (or useRoute()) and forward it to useFetch when it exists.timeout (or a project‑wide default) into the $fetch options before calling useFetch.data, pending, error, refresh) so callers experience no API change.A default of 5 seconds (5000 ms) balances responsiveness with typical upstream latency while avoiding premature aborts on slower networks or during lengthy SSR renders. This value can be overridden globally via $fetch.defaults.timeout = 5000 or per request by passing { timeout: 3000 } in the options argument.
When useFetchWithTimeout runs on the server, the router‑provided AbortSignal is undefined, so the composable simply skips the abort‑signal forwarding step and relies entirely on the timeout value supplied through $fetch options. No additional logic is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.