是否可以为提供JS内容的PHP文件指定缓存验证程序?

时间:2017-06-24 22:03:22

标签: javascript php .htaccess caching

我尝试了各种方法,到目前为止在.htaccess中有以下内容:

ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresDefault "access plus 1 month"

# 1 month exp
ExpiresActive On
<filesMatch ".(gpdf|PDF|gif|ico|jpg|jpeg|png|GIF|ICO|JPG|JPEG|PNG|css|js|woff|CSS|JS|WOFF|ttf|TTF|txt|TXT|html|HTML)$">
  Header unset Set-Cookie
  Header set Cache-Control "max-age=2592000"
  Header unset ETag
  FileETag None
</filesMatch>

提供JS内容的PHP文件包含以下标题:

header('Content-type: application/javascript');
header("Cache-control: max-age=2592000, must-revalidate");

当我去进行速度测试时,它仍显示“为js.php指定缓存验证器”

我正在做什么,或者我只是错过了什么?

修改 相关文件的响应标头:

HTTP/1.1 200 OK
Date: Sat, 24 Jun 2017 23:14:33 GMT
Server: Apache/2.4.23 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
X-Powered-By: PHP/5.6.26
Vary: Accept-Encoding
Content-Encoding: gzip
Cache-Control: max-age=2592000, must-revalidate, max-age=2592000
Expires: Mon, 24 Jul 2017 23:14:33 GMT
Connection: keep-alive, Keep-Alive
Content-Length: 7436
Keep-Alive: timeout=30, max=100
Content-Type: application/javascript

1 个答案:

答案 0 :(得分:2)

感谢@ user82217,我已经设法找出答案。

我缺少的一行是php文件中最后修改过的标题。

我有:

# setting empty nan column for 'val' in df1
df1['val'] = np.nan

# iterate through each row in df1
for index, row in df1.iterrows():
    # look for corresponding rows in temp dataframe 
    # that matches id of current row of df1
    val_df = temp.loc[temp['id'] == row['id']]
    # if id matched in temp then it will return non-empty
    if not val_df.empty:
         # assign corresponding row of df1 with first value 
         # of temp which where id matched
        df1.loc[index, 'val'] = val_df['val'].values[0]

需要追加:

header('Content-type: application/javascript');
header("Cache-control: max-age=2592000, must-revalidate");

这为文件提供了GMT中正确格式的最后修改标题,用于正在执行的脚本文件。

这解决了我的问题!