浏览器不会读取更新的 CSS
编辑:诚挚的歉意!除了我自己之外,这不是任何问题 - 我有一个 global.css 文件,其中包含正确的内容,但在其下面,我在 我的一些 HTML。 Facepalm。
我正在开发一个网站。我正在使用 LESS 来增强我的 CSS,使其更容易编写。问题是,当我更改 .less
文件时,浏览器中呈现的样式拒绝更改。我查看了生成的 .css
文件,并更新以反映所做的更改,但是浏览器不会更新 CSS 文件中的渲染样式。我已经在 Chrome、FF(3 和 4)和 Opera 中尝试过此操作,具有相同的非更新结果。
我什至告诉浏览器不缓存任何内容,无论是 PHP 还是元标记,都无济于事。我的 Apache 配置文件几乎是普通的,尽管我使用多个本地主机(这是本地服务器)。下面给出了用于将 LESS 转换为 CSS 的代码,并且每次重新加载页面时都会运行该代码:
try
{
lessc::ccompile('global/global.less', 'global/global.css');
}
catch(exception $ex)
{
exit('lessc fatal error:<br />' . $ex->getMessage());
}
这里没有例外。 less.php 解析器检查文件是否已被修改,我将其删除了一些,但每次更改时都会重新生成 CSS 文件,因此这一定是浏览器某处的缓存问题... Apache 很好地提供了更新后的 CSS 文件 :-/
抱歉说了这么久,但我想澄清一下。如果您还需要任何其他信息,请告诉我。
EDIT: My sincere apologies! This wasn't an issue with anything but myself - I had a global.css file with correct stuff in it, but below that I included another file with the old CSS in it, in the <head>
bit of my HTML. Facepalm.
I have a site I'm developing. I'm using LESS to enhance my CSS to make it easier to write. The problem is, when I change the .less
file, the styles rendered in the browser refuse to change. I've looked in the generated .css
file, and that updates to reflect the changes made, however the browser doesn't update it's rendered style from the CSS file. I've tried this in Chrome, FF(3 and 4) and Opera, with the same non-updating results.
I've even told the browser to cache nothing, both with PHP and meta tags, to no avail. My Apache config file is almost vanilla, although I am using multiple localhosts (this is a local server). The code used to convert LESS to CSS is given below, and is run every time the page is reloaded:
try
{
lessc::ccompile('global/global.less', 'global/global.css');
}
catch(exception $ex)
{
exit('lessc fatal error:<br />' . $ex->getMessage());
}
There are no exceptions here. the less.php
parser checks if the file has been modified, which I removed for a bit, but the CSS file is re-generated on every change, so this must be a caching issue with the browser somewhere... Apache serves up the updated CSS file just fine :-/
Sorry to go on for so long, but I wanted to be clear. If you need anything else, do let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一次我在代码中看到使用时间戳来强制浏览器在每次请求时下载
css
和js
文件,这样:?ts=123456789< /code> 每当数字与前一个不同时强制浏览器重新加载文件。
所以我采用了这个想法,但是我使用了文件
style.css
修改的时间戳,而不是now
的时间戳;因此它会缓存在浏览器中,直到在服务器上进行修改:Once I saw in a code the use of timestamp to force the browser to download the
css
andjs
files every request, that way:The
?ts=123456789
forces the browser to reload the file whenever the number is different from the previous one.So I adopted the idea, but instead of timestamp of
now
, I use timestamp of the modification of filestyle.css
; so it's cached in the browser until be modified on the server:我正在使用 LESS 和 Laravel,我终于找到了一个很好的解决方案:
在我的
标签中,我有:
然后我还创建了FileHelper 类(基于 https://stackoverflow.com/a/6767411/470749):
我可能决定使用这种方法仅在我的本地开发服务器上使用,并使用不同的生产方法,以便它并不总是在每个页面加载时检查文件时间戳。
I'm using LESS and Laravel, and I finally figured out a good solution:
In my
<head>
tag, I have:<link rel="stylesheet/less" type="text/css" href="/less/main.less?ts={{FileHelper::getMostRecentModifiedTimeInFolder(realpath(public_path() . '/less'))}}" />
Then I also created a FileHelper class (based on https://stackoverflow.com/a/6767411/470749):
I might decide to use this approach only on my local development server and use a different approach for production so that it's not always checking file timestamps on every page load.
由于您无法控制浏览器缓存,我建议您为 css 文件提供版本。像global.1.11.css。
Since you can't control browser cache, I would suggest you give versions to your css file. Like global.1.11.css.