设置 Clear-Site-Data 标头时的预期行为?

IMB*_*IMB 8 http cache-control browser-cache http-headers clear-site-data

根据文档

Clear-Site-Data 标头清除与请求网站关联的浏览数据(cookie、存储、缓存)

现在尝试一下,您可以在屏幕截图(Firefox v76)中看到,在Clear-Site-Data浏览器中的“响应”部分中进行了设置,但是您仍然可以看到资产“已缓存”:

注意:即使在一段时间后来回导航后,缓存的资源似乎也没有被清除。

在此输入图像描述

我的印象是这会立即发生,但我无法让它发挥作用。这应该立即发生还是在一段时间后发生,或者我只是错过了其他一些事情?


为关心的人更新:

Clear-Site-Data似乎只适用于localhosthttps

Joe*_*Joe 5

这应该立即发生还是在一段时间后发生,或者我只是错过了其他一些事情?

它应该立即发生。规范(草案)指出

如果Clear-Site-Data标头存在于从网络接收的 HTTP 响应中,则在将响应呈现给用户之前必须清除数据。

此外,正如您在此评论中提到的,仅当请求是安全的( 或 )时才支持httpslocalhost

我准备了一个简单的测试,有两个资源:

  • index.html-- 链接到 CSS 文件的页面,还接受查询参数以在响应中?clear包含标头CSD
  • style.css-- 具有随机颜色的 CSS 页面,以明确何时重新生成,并声明自身为可缓存

这与 Firefox 76.0.1 中指定的行为相同;在接收带有 的资源时Clear-Site-Data: "cache",在获取其子资源之前会清除缓存。

没有Clear-Site-Data

  • index.html通过输入 URL 并点击来获取Enter
  • 重复此操作。请注意,引用style.css是从缓存中提供的,并且页面颜色不会改变

Clear-Site-Data

  • index.html?clear通过输入 URL 并点击来获取Enter
  • 重复此操作。请注意,引用的内容style.css不是从缓存中提供的,并且页面颜色会发生变化

代码:

#!/usr/bin/python3

import http.server
import socketserver

import random

PORT = 8000

class SampleDataHandler(http.server.SimpleHTTPRequestHandler):

    def do_GET(self):
        if ".css" in self.path:
            self.send_response(200)
            self.send_header('Content-Type', 'text/css')
            self.send_header('Cache-Control', 'max-age=3600')
            self.end_headers()
            color = b"%06x" % random.randint(0, 0xFFFFFF)
            self.wfile.write(b"html{background-color: " + color + b";}\n")
        else:
            self.send_response(200)
            if '?clear' in self.path:
                self.send_header('Clear-Site-Data', '"cache"')
            self.end_headers()
            self.wfile.write(b"<link rel=stylesheet href=style.css>This is the content.\n")


httpd = socketserver.TCPServer(("", PORT), SampleDataHandler)

httpd.serve_forever()
Run Code Online (Sandbox Code Playgroud)

  • 好发现![这一行](https://hg.mozilla.org/integration/mozilla-inbound/rev/015ed5271d5d#l6.202)似乎在Firefox中断言,并且对于不被视为“安全”的URI来说,这是一个无操作”。它可能应该与[仅限于安全上下文的功能](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts/features_restricted_to_secure_contexts)一起列出。 (2认同)