【问题标题】:Enable Caching of Javascript, CSS files which are included in a JSP file启用 JSP 文件中包含的 Javascript、CSS 文件的缓存
【发布时间】:2017-09-06 15:06:05
【问题描述】:

我正在尝试启用浏览器缓存我的 Web 应用程序的 javascript、css 文件。我使用的文档类型是 JSP。

我看到 manifest.appcache 已弃用,实现缓存的最佳方法是什么,以便浏览器缓存 js、css 文件。

下面是我的代码

<html manifest="../manifest.appcache">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Web app</title>
    <link rel="stylesheet" href="<%= contextPath %>/app.css">
</head>
<body>
    <script type="text/javascript" src="<%= contextPath %>/app.js"></script>
</body>
</html>

【问题讨论】:

  • 您确定路径存在吗?用纯字符串输入路径会得到什么
  • 是的,它存在,我删除了@include文件只是为了保持干净,路径是resources/app/app.js,app.css
  • 在体内执行&lt;p&gt;&lt;%= contextPath %&gt;&lt;/p&gt;会得到什么
  • localhost:8080/resources
  • localhost:8080/resources + /app.js 组成localhost:8080/resources/app.js 你说整个路径是resources/app/app.js 你缺少/app 吗?!

标签: javascript jsp browser browser-cache cache-control


【解决方案1】:

您必须将 Expires 添加到您的 http 响应标头。这将使浏览器在客户端保存页面的组件直到该日期。 如果您想知道如何在发送响应时设置标头,您必须知道使用后端编程语言或第三方来配置您的服务器。

请参阅 expires header

尝试将此添加到您的 html 文件 <meta http-equiv="Cache-control" content="public">

【讨论】:

  • 也许您的客户正在发送Cache-Control: no-cache,请参阅stackoverflow.com/questions/14541077/…
  • 尝试将请求头设置为Cache-Control: max-age=2592000@Sai,这都是关于请求头的。
  • 类似这样的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2023-04-04
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多