Domexception:在添加或检索过程中,操作是不安全的
我想将缓存添加到我的网站。我正在使用以下两个函数添加和获取缓存。它在开发过程中运行良好,但是当我使用Docker构建应用程序时,缓存就无法正常工作。它给我带来了以下错误。(未求(在承诺)domexception:操作是不安全的。)
这是我的代码。
export const addDataIntoCache = (cacheName, url, response) => {
// Converting our response into Actual Response form
const data = new Response(JSON.stringify(response));
if ('caches' in window) {
// Opening given cache and putting our data into it
caches.open(cacheName).then((cache) => {
cache.put(url, data);
console.log('Data Added into cache!')
});
} else {
console.log("does not able to find caches in window");
caches.open(cacheName).then((cache) => {
cache.put(url, data);
console.log('Data Added into cache!')
});
}
};
export const getSingleCacheData = async (cacheName, url) => {
if (typeof caches === 'undefined') {
console.log("cache is undefined");
return null;
}
const cacheStorage = await caches.open(cacheName);
const cachedResponse = await cacheStorage.match(url);
// If no cache exists
if (!cachedResponse || !cachedResponse.ok) {
console.log('Fetched failed!');
return null;
}
return cachedResponse.json().then((item) => {
console.log("fetched from cache");
console.log(item);
return item;
});
};
I want to add cache to my site. I am using the following two functions to add and get the cache. It is working fine during development but when I use docker to build the app, then the caching is not working. It is giving me the following error.(Uncaught (in promise) DOMException: The operation is insecure.)
Here is my code.
export const addDataIntoCache = (cacheName, url, response) => {
// Converting our response into Actual Response form
const data = new Response(JSON.stringify(response));
if ('caches' in window) {
// Opening given cache and putting our data into it
caches.open(cacheName).then((cache) => {
cache.put(url, data);
console.log('Data Added into cache!')
});
} else {
console.log("does not able to find caches in window");
caches.open(cacheName).then((cache) => {
cache.put(url, data);
console.log('Data Added into cache!')
});
}
};
export const getSingleCacheData = async (cacheName, url) => {
if (typeof caches === 'undefined') {
console.log("cache is undefined");
return null;
}
const cacheStorage = await caches.open(cacheName);
const cachedResponse = await cacheStorage.match(url);
// If no cache exists
if (!cachedResponse || !cachedResponse.ok) {
console.log('Fetched failed!');
return null;
}
return cachedResponse.json().then((item) => {
console.log("fetched from cache");
console.log(item);
return item;
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论