Timeout limits for Vue Storefront commerce calls: what to consider?
0 reputation · 04 Apr 2020, 20:50 UTC
0 reputation · 04 Apr 2020, 20:50 UTC
Gracefully handle request cancellations and timeouts when calling commerce backends through Vue Storefront’s SDK/middleware layer.
Vue Storefront routes all storefront calls through a server‑side middleware that wraps an HTTP client (commonly axios). The timeout and cancellation behavior is therefore defined by the client configuration and the lifecycle of the component or page rendering. SSR responses can fail with 500 errors if a backend hangs, while client‑side navigation may leave stale requests unresolved if not aborted.
The framework does not prescribe a single pattern for cancellation: developers can abort requests per composable, use Vue/Nuxt lifecycle hooks, or centralize it via an interceptor. Additionally, the exact keys for configuring per‑endpoint timeouts in the middleware configuration vary across Vue Storefront releases.
31775 reputation · 04 Apr 2020, 21:19 UTC
Vue Storefront’s middleware exposes a timeout property per integration in middleware.config.js (or middleware.config.ts). That value is applied as axios.defaults.timeout only for that backend, so you can set different limits for each commerce API.
Example middleware.config.js snippet:
module.exports = {
integrations: {
magento: {
url: 'https://api.magento.local',
timeout: 20000, // 20 s for Magento calls
},
shopify: {
url: 'https://api.shopify.local',
timeout: 12000, // 12 s for Shopify calls
},
},
};
If your Vue Storefront release predates the timeout key, add a request‑level interceptor that sets config.timeout based on the request URL.
Two common patterns:
AbortController signal to every request. Tie the controller to the Nuxt route or a Pinia store flag so that navigating away aborts all pending calls.cancel function from each composable (e.g., useProduct) and let the component decide when to abort. This gives fine‑grained control but requires explicit cancellation at each call site.A hybrid approach works well: a global interceptor supplies a default signal, while composables can override it or provide an explicit signal if needed.
Axios propagates aborted requests as a distinct error:
error.code === 'ERR_CANCELED'error.name === 'CanceledError'Time‑outs produce error.code === 'ECONNABORTED' or ETIMEDOUT, while genuine network failures show codes like ENOTFOUND or EHOSTUNREACH. Inspecting error.code (or error.name in the browser) lets you distinguish an intentional abort from a real failure and render an appropriate fallback UI.
timeout key was introduced in v2.0. If you’re on v1.x, use a request interceptor.middleware.config.js as shown above.AbortController signal tied to the Nuxt context:
axios.interceptors.request.use((config) => {
const controller = new AbortController();
config.signal = controller.signal;
config.meta = { controller };
return config;
});
const controller = new AbortController();
axios.get(url, { signal: controller.signal });
return { cancel: () => controller.abort() };
error.code and rendering a 500 page or a user‑friendly message accordingly.To tailor the recommendation precisely, could you confirm which Vue Storefront version you’re using (v1.x or v2.x)? This determines whether the timeout key is natively supported or if an interceptor is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.