使用 JavaScript Axios/Fetch。你能禁用浏览器缓存吗?
Using JavaScript Axios/Fetch. Can you disable browser cache?
我正在尝试为我正在更新到 React.js 的 freeCodeCamp 项目查询报价 API。我现在正在尝试使用 Fetch
或 Axios
来查询 API 但它在浏览器中缓存响应。我知道 $ajax
中有一个 { cache: false }
会强制浏览器执行新请求。
有什么方法可以让我对 Fetch
或 Axios
做同样的事情吗?
cache-control
设置似乎已被 Axios
设置为 max-age: 0
。
这是我查询 API.
的代码
generateQuote = () => {
axios.get('https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1')
.then(response => {
const { title, content, link } = response.data[0];
console.log(title, content, link)
this.setState(() => ({ title, content, link }));
})
.catch(err => {
console.log(`${err} whilst contacting the quote API.`)
})
}
好的,所以我找到了解决方案。我必须在 API url 上设置一个时间戳才能让它拨打新电话。似乎没有办法强制 axios
或 fetch
禁用缓存。
这就是我的代码现在的样子
axios.get(`https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1×tamp=${new Date().getTime()}`)
.then(response => {
const { title, content, link } = response.data[0];
console.log(title, content, link)
this.setState(() => ({ title, content, link }));
})
.catch(err => {
console.log(`${err} whilst contacting the quote API.`)
})
我认为您每次进行 axios 调用时只需要使 url 不同即可。时间戳只是这样做的一种方式。如果您正在开发 PWA,还请考虑禁用或过滤 Service Worker 缓存方法。
我将这些 headers 添加到所有 axios 请求中并且运行良好。
axiosInstance.defaults.headers = {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
};
看来,添加时间戳是唯一可行的方法。
如果您使用的是 Vue,例如:
const api = axios.create({
baseURL: 'https://example.com/api',
params: {
t: new Date().getTime()
}
})
Vue.prototype.$api = api
因此您可以将其用于:
this.$api.get('items')
并且它总是会根据当前请求时间向 url 添加不同的时间戳。
创建一个axios实例,然后为每个请求添加时间戳。
const axiosInstance = axios.create({})
axiosInstance.interceptors.request.use(
function (config) {
// Do something before request is sent
config.params = { ...config.params, timestamp: Date.now() };
return config;
},
function (error) {
// Do something with request error
return Promise.reject(error);
}
);
如果您不想对所有 axios 请求禁用缓存,您可以在 axios
调用中使用以下参数来仅对一个请求禁用缓存:
axios.get(
'https://YOUR-URL.com',
{
// query URL without using browser cache
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
},
}
)
我正在尝试为我正在更新到 React.js 的 freeCodeCamp 项目查询报价 API。我现在正在尝试使用 Fetch
或 Axios
来查询 API 但它在浏览器中缓存响应。我知道 $ajax
中有一个 { cache: false }
会强制浏览器执行新请求。
有什么方法可以让我对 Fetch
或 Axios
做同样的事情吗?
cache-control
设置似乎已被 Axios
设置为 max-age: 0
。
这是我查询 API.
的代码generateQuote = () => {
axios.get('https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1')
.then(response => {
const { title, content, link } = response.data[0];
console.log(title, content, link)
this.setState(() => ({ title, content, link }));
})
.catch(err => {
console.log(`${err} whilst contacting the quote API.`)
})
}
好的,所以我找到了解决方案。我必须在 API url 上设置一个时间戳才能让它拨打新电话。似乎没有办法强制 axios
或 fetch
禁用缓存。
这就是我的代码现在的样子
axios.get(`https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1×tamp=${new Date().getTime()}`)
.then(response => {
const { title, content, link } = response.data[0];
console.log(title, content, link)
this.setState(() => ({ title, content, link }));
})
.catch(err => {
console.log(`${err} whilst contacting the quote API.`)
})
我认为您每次进行 axios 调用时只需要使 url 不同即可。时间戳只是这样做的一种方式。如果您正在开发 PWA,还请考虑禁用或过滤 Service Worker 缓存方法。
我将这些 headers 添加到所有 axios 请求中并且运行良好。
axiosInstance.defaults.headers = {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
};
看来,添加时间戳是唯一可行的方法。
如果您使用的是 Vue,例如:
const api = axios.create({
baseURL: 'https://example.com/api',
params: {
t: new Date().getTime()
}
})
Vue.prototype.$api = api
因此您可以将其用于:
this.$api.get('items')
并且它总是会根据当前请求时间向 url 添加不同的时间戳。
创建一个axios实例,然后为每个请求添加时间戳。
const axiosInstance = axios.create({})
axiosInstance.interceptors.request.use(
function (config) {
// Do something before request is sent
config.params = { ...config.params, timestamp: Date.now() };
return config;
},
function (error) {
// Do something with request error
return Promise.reject(error);
}
);
如果您不想对所有 axios 请求禁用缓存,您可以在 axios
调用中使用以下参数来仅对一个请求禁用缓存:
axios.get(
'https://YOUR-URL.com',
{
// query URL without using browser cache
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
},
}
)