Domexception:在添加或检索过程中,操作是不安全的

发布于 2025-01-26 10:13:58 字数 1409 浏览 2 评论 0原文

我想将缓存添加到我的网站。我正在使用以下两个函数添加和获取缓存。它在开发过程中运行良好,但是当我使用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.)

Image showing the error

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文