简体   繁体   English

不以.css结尾的CSS文件

[英]CSS files that don't end with .css

Is there a disadvantage to using a dynamic Python file to generate the CSS for a webpage? 使用动态Python文件为网页生成CSS是否有缺点? I'd like computers with an administrator cookie to show special admin panel CSS, and show regular CSS for all other users. 我希望带有管理员cookie的计算机能够显示特殊的管理面板CSS,并为所有其他用户显示常规CSS。 I'm planning to use: 我打算用:

<link rel="stylesheet" href="/css.py" type="text/css" />

That should work fine. 这应该工作正常。

I hope , however, that you're not relying on CSS alone to restrict admin functionality. 但是,我希望您不仅仅依靠CSS来限制管理功能。

You can do that. 你可以做到这一点。 Just realize that there will be a performance hit on each page. 只要意识到每个页面都会有性能损失。 CSS files are usually cached as they do not change often. CSS文件通常是缓存的,因为它们不会经常更改。

In theory you do not need to even use an <link> as you can render the style right in the page as a <style> , as it will be refreshed every page request. 从理论上讲,您甚至不需要使用<link>因为您可以将页面中的<style>作为<style>呈现,因为它将在每个页面请求时刷新。

  • You will need to specify the correct content type when generating the resource 生成资源时,您需要指定正确的内容类型
  • You will probably need to be explicit about issuing cache control headers 您可能需要明确说明发出缓存控制标头

It probably isn't worth the effort. 这可能不值得努力。 If there is a lot of admin panel specific CSS, then just have a second <link> element if logged in as an admin, otherwise just merge it with the main file. 如果有很多特定于管理面板的CSS,那么如果以管理员身份登录,则只需要第二个<link>元素,否则只需将其与主文件合并即可。

Extensions don't matter. 扩展无关紧要。 You can end it in .jpg if you want. 如果需要,您可以以.jpg结尾。 What does (somehow) matter are the headers. 什么 (在某种程度上)无论是头部。 You should send the following header: 您应该发送以下标题:

Content-Type: text/css; charset=UTF-8

Still, if you don't add that, you won't run into any (immediate) problems. 但是,如果你不添加它,你将不会遇到任何(立即)问题。 As long as you use type="text/css" in HTML the browser will know what you're talking about. 只要您在HTML中使用type="text/css" ,浏览器就会知道您在说什么。

Note that, as Glennular said, CSS files are cached. 请注意,正如Glennular所说,CSS文件是缓存的。 Thus, you might want to use something like: 因此,您可能希望使用以下内容:

<link rel="stylesheet" href="/css.py?SOME_RANDOM_SEED" type="text/css" />

Where SOME_RANDOM_SEED is, obviously, a random character sequence. 显然, SOME_RANDOM_SEED是一个随机字符序列。 You can use time.time() or uuid.uuid4() or something similar. 您可以使用time.time()uuid.uuid4()或类似的东西。

The main disadvantage is that most caching systems (both client-side browser caching and server-side caching systems) assume that CSS is static, and thus might cache the first call of the CSS file (css.py), and then won't update it again. 主要的缺点是大多数缓存系统(客户端浏览器缓存和服务器端缓存系统)都假设CSS是静态的,因此可能会缓存CSS文件的第一次调用(css.py),然后不会再次更新它。

If you use a random seed, as @Felix suggested, your file will not be cached (so your solution would work as expected), but then your file will not be cached (so you lose performance benefits). 如果您使用随机种子,正如@Felix建议的那样,您的文件将不会被缓存(因此您的解决方案将按预期工作),但之后您的文件将不会被缓存(因此您将失去性能优势)。 That is, add a randomly-generated (eg time stamp) query string to the CSS file name (eg css.py?201309102358). 也就是说,将随机生成的(例如时间戳)查询字符串添加到CSS文件名(例如css.py?201309102358)。

For the best of both worlds (dynamic CSS + caching of the dynamic CSS), you could do an Apache URL rewrite as described by Bryan Headrick. 对于两全其美(动态CSS的动态CSS +缓存),您可以按照Bryan Headrick的描述进行Apache URL重写 In brief, his solution involves using a CSS filename that looks legitimate, but with the parameters as part of the name eg "white-2.css" where "white" indicates a theme with a white background and "2" indicates a two-column format. 简而言之,他的解决方案涉及使用看起来合法的CSS文件名,但参数作为名称的一部分,例如“white-2.css”,其中“white”表示具有白色背景的主题,“2”表示两个 - 列格式。 The Apache rewrite would convert any URL of the format " _ -_.css" into a dynamic filename like "dynamiccss.py?background=white&columns=2". Apache重写会将格式为“ _ -_。css”的任何网址转换为动态文件名,例如“dynamiccss.py?background=white&columns=2”。 Caching agents (client browser caching and server-side caching) would only see the "white-2.css" version, and so would cache it as normal. 缓存代理(客户端浏览器缓存和服务器端缓存)只会看到“white-2.css”版本,因此会正常缓存它。 Only Apache would see and redirect the dynamiccss.py version, which receives the necessary parameters to do its magic. 只有Apache才会看到并重定向dynamiccss.py版本,该版本接收必要的参数以实现其魔力。 Bryan Headrick's explanation uses PHP and Apache, but the logic should be applicable to any server-side scripting language (eg Python) and web server. Bryan Headrick的解释使用PHP和Apache,但逻辑应该适用于任何服务器端脚本语言(例如Python)和Web服务器。

The downside to using a dynamic file is that it won't be as fast as a static file. 使用动态文件的缺点是它不会像静态文件那么快。 In all likelihood, that's not really a problem (the difference is likely negligible). 很可能,这不是一个真正的问题(差异可能是微不足道的)。 If it later turns out it is a problem, you could just precompute the different CSS and link in the appropriate one. 如果它后来证明这是一个问题,你可以预先计算不同的CSS和链接在适当的。

On an unrelated note, it's not really safe to trust a cookie to determine if the user is an administrator. 在一个不相关的说明中,信任cookie以确定用户是否是管理员是不安全的。

If www.example.com/css.py is accessible by a web browser and produces valid css it should work, at least with php it does. 如果Web浏览器可以访问www.example.com/css.py并生成有效的CSS,它应该可以工作,至少用php可以。 maybe you just need to pass a content-type: text/css HTTP header 也许你只需要传递一个content-type: text/css HTTP标头

However I think you could set special classes in the HTML page generation in case of this cookie presence and let a static css handle them, but if you really need to do this I see no other disadvantage apart than a little more processing time. 但是我认为你可以在这个cookie存在的情况下在HTML页面生成中设置特殊类,并让静态css处理它们,但是如果你真的需要这样做,除了更多的处理时间之外我没有看到任何其他缺点。

Although using dynamically created CSS works nowadays, I fear that you can't rely on it being changed depending on a cookie. 虽然现在使用动态创建的CSS可以工作,但我担心你不能依赖它根据cookie来改变它。

CSS files are cached more heavily than the pages themselves, so even if you try to prevent the file from being cached, some browsers might hold on to an old version longer than they should. CSS文件的缓存比页面本身更多,因此即使您尝试阻止文件被缓存,某些浏览器也可能会比旧版本更长时间地保留旧版本。

So, if you are thinking of using the CSS for showing or hiding features depending on a cookie, that's not a stable solution. 因此,如果您正在考虑使用CSS根据cookie显示或隐藏功能,那么这不是一个稳定的解决方案。

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

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