Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 缓存存储API,can';我没有头球回来_Javascript_Google Chrome_Service Worker_Browser Cache - Fatal编程技术网

Javascript 缓存存储API,can';我没有头球回来

Javascript 缓存存储API,can';我没有头球回来,javascript,google-chrome,service-worker,browser-cache,Javascript,Google Chrome,Service Worker,Browser Cache,我想检查最大年龄,以便在项目变旧时将其从缓存中删除,但由于某些原因,我无法获取自己的标题 export function cacheResponse(url, response) { caches.open('my-cache').then(function(cache) { cache.put(url, new Response(JSON.stringify(response), { headers: {

我想检查最大年龄,以便在项目变旧时将其从缓存中删除,但由于某些原因,我无法获取自己的标题

  export function cacheResponse(url, response) {
          caches.open('my-cache').then(function(cache) {
            cache.put(url, new Response(JSON.stringify(response), {
              headers: {
                'Cache-Control': 'max-age=1',
                'Content-Type': 'application/json;charset=UTF-8'
              }
            }));
          });
        }

cacheResponse('/hello', {hello: "world"})
我可以在chrome的
应用程序
选项卡中看到这一点,并且我可以在预览中看到这两个标题,但是当我将该项目拉出时,
标题
对象为空

cache.match(url).then(async function(object) {
  console.log(object.headers) // null
  var body = await object.clone().json(); // {hello: "world"}
})
对象看起来像这样

object: Response
body: ReadableStream
bodyUsed: false
headers: Headers
__proto__: Headers
ok: true
redirected: false
status: 200
statusText: ""
type: "default"
url: ""

似乎我应该能够通过调用match()no?

来查找标题,这应该是可行的;您应该能够调用
response.headers.get('cache-control')
来检索该值(假设这是一个相同的原始响应)

下面是我刚刚测试的一段代码,在JS控制台中运行时可以正常工作:

async function test() {
  const constructedResponse = new Response('test', {
    headers: {'cache-control': 'max-age=1'}
  });

  const cache = await caches.open('test');

  cache.put('/test', constructedResponse);

  const matchedResponse = await cache.match('/test');

  console.log(`cache-control: ${matchedResponse.headers.get('cache-control')}`);
}

这应该行得通;您应该能够调用
response.headers.get('cache-control')
来检索该值(假设这是一个相同的原始响应)

下面是我刚刚测试的一段代码,在JS控制台中运行时可以正常工作:

async function test() {
  const constructedResponse = new Response('test', {
    headers: {'cache-control': 'max-age=1'}
  });

  const cache = await caches.open('test');

  cache.put('/test', constructedResponse);

  const matchedResponse = await cache.match('/test');

  console.log(`cache-control: ${matchedResponse.headers.get('cache-control')}`);
}