强制 Firefox 缓存特定时间段的 URL

dav*_*des 5 networking firefox firefox-extensions browser-cache

我合作的其中一家公司有一个通过慢速 DSL 线路托管的 Web 应用程序。他们的服务器配置或他们使用的软件版本存在问题,这意味着每次访问页面都会触发下载大量 JavaScript 库。根据 Firebug,每个页面的实际内容交付相对较快。

有没有办法强制 Firefox 将特定 URL 视为具有较长的到期期限?

许多年前,我曾经在使用自定义正则表达式规则的鱿鱼代理安装上执行此类操作,以强制为行为不端的站点设置最小缓存时间。我正在尝试做的事情似乎很相似,但纯粹是客户端。

Wla*_*ant 3

操作特定的缓存条目实际上是不可能的,甚至 Firefox 扩展也没有该级别的访问权限。但是,可以操纵服务器返回的标头。不幸的是,所有现有的扩展都专注于操作请求标头而不是响应标头。但对此的扩展非常简单。因此,也许您想在再次安装 Squid 之前尝试该方法。这是类似扩展的代码:

安装.rdf

<?xml version="1.0" encoding="utf-8"?>

<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    <em:id>headermanip@superuser.com</em:id>
    <em:version>1.0</em:version>
    <em:type>2</em:type>
    <em:bootstrap>true</em:bootstrap>

    <!-- Firefox -->
    <em:targetApplication>
      <Description>
        <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
        <em:minVersion>8.0</em:minVersion>
        <em:maxVersion>99.0</em:maxVersion>
      </Description>
    </em:targetApplication>

    <!-- Front End MetaData -->
    <em:name>Caching header manipulation</em:name>
  </Description>
</RDF>
Run Code Online (Sandbox Code Playgroud)

bootstrap.js

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function install(data, reason) {}
function uninstall(data, reason) {}

function startup(data, reason)
{
  Services.obs.addObserver(Observer, "http-on-examine-response", true)
};

function shutdown(data, reason)
{
  Services.obs.removeObserver(Observer, "http-on-examine-response")
};

var Observer =
{
  observe: function(subject, topic, data)
  {
    if (subject instanceof Components.interfaces.nsIHttpChannel &&
        subject.URI.host == "cdn.sstatic.net")
    {
      subject.setResponseHeader("Cache-Control", "max-age=2592000", false);
    }
  },

  QueryInterface: XPCOMUtils.generateQI([
    Components.interfaces.nsIObserver,
    Components.interfaces.nsISupportsWeakReference
  ])
};
Run Code Online (Sandbox Code Playgroud)

将这两个文件放入 ZIP 存档中并将其重命名为headermanip.xpi- 完成,您就有了一个扩展,它将更改来自(在本网站上使用)的任何内容的缓存标头,cdn.sstatic.net使其在 30 天后过期,而不是通常的 7 天。如果您希望它发生在不同的主机上 - 更改subject.URI签入bootstrap.js。您还可以检查subject.URI.spec是否要查看完整的 URL 而不仅仅是主机。