CSS files that do not end with .css

Is there a downside to using a Python dynamic file to generate CSS for a web page? I would like computers with an admin cookie to display a custom admin css panel and show normal css to all other users. I am planning to use:

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

      

+2


a source to share


8 answers


The downside to using a dynamic file is that it won't be as fast as a static file. In all likelihood, this is not a problem (the difference is probably not significant). If it turns out later that this is a problem, you can simply precompress the various CSS and links in the appropriate one.



In an unrelated note, it is not safe to trust a cookie to determine if the user is an administrator.

0


a source


This should work fine.



I hope, however, that you are not relying solely on CSS to limit admin functionality.

+9


a source


Can you do it. Just understand that there will be performance on every page. CSS files are usually cached because they don't change frequently.

In theory, you don't even need to use <link>

, as you can render the style on the page like <style>

, since it will update every page request.

+7


a source


  • You need to specify the correct content type when creating the resource
  • You will probably need to explicitly specify the cache control headers

It's probably not worth the effort. If there are many CSS admins, then there is only the second element <link>

if logged in as admin, otherwise just merge it with the main file.

+4


a source


Extensions don't matter. You can put it in .jpg

if you like. What does (somehow) is the header. You should send the following header:

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

      

However, if you don't add this, you won't run into any (immediate) problems. As long as you are using type="text/css"

in HTML, the browser will know what you are talking about.

Note that as Glennular says , CSS files are cached. Thus, you can use something like:

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

      

Where SOME_RANDOM_SEED

, obviously, a random sequence of characters. You can use time.time()

either or uuid.uuid4()

or something similar.

+1


a source


If www.example.com/css.py is accessible through a web browser and produces valid css, it should work with at least php. maybe you just need to pass the content-type: text/css

HTTP header

However, I think you can set special classes in the HTML page generation in case this cookie is present and let static css handle them, but if you really need to do this, I see no other drawback other than slightly longer processing time.

0


a source


While using dynamically generated CSS is currently working, I'm afraid you can't rely on it changing depending on the 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 may hang on to the old version longer than necessary.

So, if you are thinking of using CSS to show or hide functions depending on the cookie, this is not a stable solution.

0


a source


The main disadvantage is that most caching systems (both client-side caching and server-side caching systems) assume the CSS is static and thus may cache the first call to the CSS file (css.py) and then not update it again.

If you are using random seed, as @Felix suggested, your file will not be cached (so your solution will work as expected), but then your file will not be cached (so you will lose performance benefits). That is, add a freeform query string (such as a timestamp) to the CSS file name (such as css.py?201309102358).

For the best of both worlds (dynamic CSS + dynamic CSS caching) you can do Apache URL rewriteas described by Brian Headrick. In short, his solution involves using a CSS file name that looks legitimate but with 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. The Apache attribute converts any "_-_. Css" URL to a dynamic filename such as dynamiccss.py?background=white&columns=2. Caching agents (client browser caching and server side caching) will only see the "white-2.css" version and will cache it as usual. Only Apache will see and redirect the dynamiccss.py version that gets the necessary parameters for its magic. Brian Hadrick's explanation uses PHP and Apache,but the logic should be applicable to any server-side scripting language (like Python) and web server.

0


a source







All Articles