繁体   English   中英

服务器正在缓存JS文件或嵌入式HTML脚本?

[英]Server is caching JS files or embedded HTML scripts?

我的一个站点的服务器上出现异常,我需要您的帮助以找到解决方案。

如果打开由FtpZilla下载的PHP文件并编辑任何PHP代码,则在保存并重新上传文件后,新内容将处于活动状态。 任何HTML代码都一样

如果在同一文件上编辑一些HTML代码,其中包含<script></script>标记,并且其中包含javascript代码,则当我上传文件的新版本时,旧版本的JS代码会投放到浏览器中一。 我尝试使用CTRL + F5,几个浏览器:没有出路。 同样令人失望的结果,自“开始之初”以来就清理了每个浏览器的缓存

重新打开刚刚上传的新文件,我惊讶地发现了我的新代码。 就像服务器正在提供旧文件一样。 但是为什么只有JS部分呢?

不仅:对于任何JS外部文件,我都遇到相同的问题。 我的意思是文件被标记为<script src=""></script>嵌入HTML页面

我试图取消对.htaccess的任何缓存命令,如下所示。 但是在这种情况下,也没有更好的事情发生。

我可以进一步做什么?

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
# Enable expirations.
  ExpiresActive On

      # Cache all files for 2 weeks after access (A).
      #ExpiresDefault A1209600

      <FilesMatch \.php$>
        # Do not allow PHP scripts to be cached unless they explicitly send cache
        # headers themselves. Otherwise all scripts would have to overwrite the
        # headers set by mod_expires if they want another caching behavior. This may
        # fail if an error occurs early in the bootstrap process, and it may cause
        # problems if a non-Drupal PHP file is installed in a subdirectory.
        ExpiresActive Off
      </FilesMatch>

        #ExpiresByType image/jpg "access 1 year"
        #ExpiresByType image/jpeg "access 1 year"
        #ExpiresByType image/gif "access 1 year"
        #ExpiresByType image/png "access 1 year"
        #ExpiresByType text/css "access 1 month"
        #ExpiresByType application/pdf "access 1 month"
        #ExpiresByType text/x-javascript "access 1 month"
        #ExpiresByType application/x-shockwave-flash "access 1 month"
        #ExpiresByType image/x-icon "access 1 year"
        #ExpiresDefault "access 2 days"
    </IfModule>

编辑:我只是注意到:如果我创建到原始JS文件的符号链接,当我通过符号链接读取文件时(因此在浏览器中我将调用SymLink而不是文件):我仍然得到旧版本。 甚至可以通过FileZilla打开它(编辑)。 就像指针永久指向旧版本一样。 虽然如果我直接读取JS文件(直接调用它,甚至不通过嵌入式链接调用它),那么我可以获得的新版本会让我迷失更多。

由于服务器未设置任何缓存规则,因此ExpiresActive Off实际上将使您的浏览器能够缓存内容。 如果您要禁用缓存,则可以使用以下方法:

<FilesMatch "\.(html)$">
    Header set Cache-Control "private, no-cache, must-revalidate"
    Header set Pragma "no-cache"
</FilesMatch>

这将告诉您的浏览器在每个请求上检查新内容。 如果内容不是动态创建的(例如,通过php生成),并且文件自您上次收到以来没有被修改,则apache将发送304 Not Modified标头,其中没有数据(因为您的浏览器已具有最新版本)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM