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
bug: Request doesn't execute after a abortion. #317
Comments
Hey, thanks for your bug report. I can't look into it this weekend, I'll try to fix it somewhere between Monday and next weekend... |
Hey @SamueleCaprioli97, i have some bad news (for now)... Looking closely, this was the same error I got when building an axios cache interceptor react hooks library (axios-cache-hooks). Let me explain it: When a request throws any error, the response interceptor executes the axios-cache-interceptor/src/interceptors/response.ts Lines 203 to 204 in 288baa8
And these functions are stateless, so the only thing that guides us on each request it the provided error parameter. The problem is, when the error is from a cancelled request, axios does not returns any other information. (this is everything available at line 204) Coming back to The problem is that v0.27.2 seems to be our last version before 1.X, that's why it never actually got released into a new patch version (and why Our current hope is to get jasonsaayman to cherry pick this PR into a v0 patch release. When the patch gets released, automagically this problem will be fixed, so, no need to a new P.S.: Thanks for helping this package :) Edit: Look at axios/axios#4659 (comment) |
Hi @arthurfiorette, thank you for spending time on this issue. I think it's related to this portion of code in Axios: When a request is aborted before it is crafted, the Anyway i found a temporary workaround for handling aborted request that makes me able to go ahead: import axios from 'axios'
import { setupCache, buildMemoryStorage, defaultKeyGenerator } from 'axios-cache-interceptor'
const MINUTE = 1000 * 60
const memoryStorage = buildMemoryStorage()
const instance = setupCache(axios, {
methods: ['get', 'options'],
ttl: MINUTE,
storage: memoryStorage
})
instance.interceptors.request.use(function (config) {
if (!config.cache) {
return config
}
let aborted = false
if (config.cancelToken) {
if (axios.isCancel(config.cancelToken.reason)) {
aborted = true
} else {
config.cancelToken.subscribe(() => {
abortCacheHandler(config)
})
}
}
if (config.signal) {
if (config.signal.aborted) {
aborted = true
} else {
config.signal.addEventListener('abort', () => abortCacheHandler(config), { once: true })
}
}
if (aborted) {
return { ...config, ...{ cache: false } }
}
return config
})
function abortCacheHandler (config) {
const requestKey = defaultKeyGenerator(config)
memoryStorage.get(requestKey, config).then(request => {
if (request.state !== 'cached') {
memoryStorage.remove(requestKey, config)
}
})
} |
Hey @SamueleCaprioli97. I will close this issue as it is not related to something I polished the above code into a more suitable one, you can use it: import Axios from 'axios';
import { setupCache } from './cache/create';
const axios = setupCache(Axios.create());
axios.interceptors.request.use((config) => {
if (!config.cache) {
return config;
}
async function reject() {
const key = config.id ?? axios.generateKey(config);
const storage = await axios.storage.get(key, config);
// Request cancelled but response is already cached
if (storage.state === 'cached' || storage.state === 'stale') {
return;
}
await axios.storage.remove(key, config);
}
if (config.signal) {
// Already cancelled request
if (config.signal.aborted) {
config.cache = false;
return config;
}
// eslint-disable-next-line @typescript-eslint/no-misused-promises
config.signal.addEventListener('abort', reject);
}
// NOTE: Cancel token is DEPRECATED but are here for backward compatibility
// you can remove this block if you don't use cancel token.
// https://axios-http.com/docs/cancellation
if (config.cancelToken) {
// Already cancelled request
if (Axios.isCancel(config.cancelToken.reason)) {
config.cache = false;
return config;
}
void config.cancelToken.promise.then(reject);
}
return config;
}); |
Hey @SamueleCaprioli97, I reopened this PR because axios just released v1.0.0. |
Waiting for axios/axios#4922 |
Fixed! Going live with v1.0.0 |
Describe the bug
I've experienced that when i try to cancel a request and resubmit it right after, nothing seems to happen.
To Reproduce
In this CodePen, if i click the 'Request' button as first, everything seems to be fine, but after a reload, if i click first the 'With Quick Abort', all further requests will not be initiated.
Expected behavior
After the page load, when 'With Quick Abort' button is clicked, the first request will be aborted, while the second one will be sent and completed.
Additional context
P.S.: This plugin is very powerful and easy to use, I hope you can help me or tell me if I'm doing something wrong! 😊
The text was updated successfully, but these errors were encountered: