[英]How to get 304 response for PHP generated javascript file?
I have the following PHP file: 我有以下PHP文件:
<?php
Header('Content-Type: application/javascript');
echo "// Some dynamically generated JavaScript here...";
So it's a .php file but it's interpreted as a JavaScript file. 因此,它是一个.php文件,但被解释为JavaScript文件。 I would load it in a web page like so:
我将其加载到网页中,如下所示:
<script type='text/javascript' src='myjavascript.php'></script>
My question is regarding the HTTP code that is sent when accessing this file. 我的问题是关于访问此文件时发送的HTTP代码。 Every time I request this file (either directly or via a webpage that is requesting it) Chrome Dev Tools shows a Status of 200 OK.
每当我请求此文件时(直接或通过请求该文件的网页),Chrome开发工具都会显示“状态为200 OK”。 It is never a 304, even if the content hasn't changed.
即使内容没有更改,也永远不会是304。
Is it possible to get a 304 Not Modified for a dynamically generated file like this in the case that the content hasn't changed? 在内容未更改的情况下,是否可以为动态生成的文件获得“ 304未修改”? If not, why?
如果没有,为什么?
I have also used some PHP framworks that allow for routes like: /js/myjavascript.js
. 我还使用了一些
/js/myjavascript.js
,它们允许诸如/js/myjavascript.js
路由。 So I can put the above code in that route, dynamically generating the JavaScript in the same way. 因此,我可以将上述代码放入该路由中,以相同的方式动态生成JavaScript。 But again, still a 200 every time, even though the content hasn't changed AND the URI is a .js file.
但是同样,每次仍然是200,即使内容没有更改并且URI是一个.js文件。
Is there a solution for this? 有解决方案吗? In my situation, the contents of this javascript/php file changes about once a day.
以我的情况,此javascript / php文件的内容每天大约更改一次。 So I need my visitors browsers to cache the file most of the time (304 Not Modified) but when it does change, I need their browser to download and cache the new version (200 OK).
因此,我需要访问者的浏览器大部分时间来缓存文件(304未修改),但是当文件确实发生更改时,我需要他们的浏览器下载并缓存新版本(200 OK)。
The 304 Not Modified
header should only get sent if the client included an If-Modified-Since
header in the request. 仅当客户端在请求中包含
If-Modified-Since
标头时,才应发送304 Not Modified
标头。
I have a PHP script that always generates the same consistent output (it happens to be image/png) depending on what options are fed to it by a Rewrite rule. 我有一个PHP脚本,该脚本始终生成相同的一致输出(恰好是image / png),具体取决于重写规则将哪些选项提供给它。 For my script, I assume that ANY
If-Modified-Since
reflects valid cache data in the browser, so my script includes: 对于我的脚本,我假设ANY
If-Modified-Since
在浏览器中反映有效的缓存数据,因此我的脚本包括:
// If they've got it, they should use it.
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strlen($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
header("HTTP/1.0 304 Not Modified");
exit;
}
If you want to be smarter about this, you can parse the variable and compare it to whatever is important to your script. 如果您想对此变得更聪明,则可以解析变量并将其与对脚本重要的内容进行比较。 You need to think about how you will detect whether the content has changed in the context of a PHP script which dynamically generates content each time it is run.
您需要考虑如何在PHP脚本的上下文中检测内容是否已更改 ,PHP脚本在每次运行时都会动态生成内容。
For additional wisdom on how browser caching works in PHP, check out this comment on PHP: header
. 有关如何在PHP中使用浏览器缓存的其他知识,请查看有关
PHP: header
注释 。
You can test this behaviour using cURL
from a command line: 您可以从命令行使用
cURL
测试此行为:
[ghoti@pc ~]$ curl -sL -w "%{http_code} %{url_effective}\\n" https://www.google.com/images/srpr/logo3w.png -o /dev/null
200 https://www.google.com/images/srpr/logo3w.png
[ghoti@pc ~]$ curl -H"If-Modified-Since: Sat, 31 Dec 2011 00:00:00 GMT" -sL -w "%{http_code} %{url_effective}\\n" https://www.google.com/images/srpr/logo3w.png -o /dev/null
200 https://www.google.com/images/srpr/logo3w.png
[ghoti@pc ~]$ curl -H"If-Modified-Since: Mon, 31 Dec 2012 00:00:00 GMT" -sL -w "%{http_code} %{url_effective}\\n" https://www.google.com/images/srpr/logo3w.png -o /dev/null
304 https://www.google.com/images/srpr/logo3w.png
[ghoti@pc ~]$
Note the different dates. 注意不同的日期。
If you're having trouble with Chrome thinking that not enough headers have been sent, you might try duplicating what other popular sites provide in the way of headers: 如果您在使用Chrome时遇到麻烦,认为发送的标题不够,您可以尝试复制其他流行的网站以标题的方式提供的信息:
[ghoti@pc ~]$ curl -H"If-Modified-Since: Mon, 31 Dec 2012 00:00:00 GMT" -sLI https://www.google.com/images/srpr/logo3w.png
HTTP/1.1 304 Not Modified
Date: Wed, 05 Dec 2012 17:40:52 GMT
Expires: Thu, 13 Dec 2012 17:40:52 GMT
Age: 241012
Server: GFE/2.0
No doubt, Server:
can be ignored, but you may have less complaints from Chrome if you include the other three. 毫无疑问,
Server:
可以忽略不计,但是如果包括其他三个,您对Chrome的抱怨可能会更少。 Though, as I said, YOU will have to come up with the criteria you want to use to determine what "age" you consider your dynamically-generated content, since the filesystem timestamp on a static file as detected by Apache is no longer usable. 但是,正如我所说,您将不得不使用要用来确定动态生成内容的“年龄”的标准,因为由Apache检测到的静态文件的文件系统时间戳不再可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.