Jump to content

main.css?ts=<?= time() ?> Has me confused.


Greg Lumley
 Share

Recommended Posts

Hi! In busy learning to know PW better I'm looking at existing code.

I have Bitpoets Editorial Responsive Blog as example. While looking through the code I've just found this line in the blog-head.php 

<link rel="stylesheet" href="<?= $config->urls->templates ?>assets/css/main.css?ts=<?= time() ?>" />

I hope it's not a stupid question but I've never seen this before...

main.css?ts=<?= time() ?>

I notice the main template is empty but of course renders on the front end.

Using a timestamp really has me totally confused. Can someone explain it to me please. 

Thank you! 

Greg

Link to comment
Share on other sites

The idea is that the link to the css-file will change every second, and the browser will be tricked into thinking this is a new stylesheet and loading it fresh every time (and not cached copy).

  • Like 1
Link to comment
Share on other sites

There's gotta be some CSS file loading (if the site has any actual styles), check your Network tab on chrome to check what's loaded, also if you inspect any element it should indicate the style's source. Otherwise, it might have been output right into the html body. 

Link to comment
Share on other sites

9 hours ago, Greg Lumley said:

In other words if I wanted to modify any styling where would I do it? 

I don't really know that site profile, but looking at the repository there's a SASS folder, you're probably supposed to compile the CSS from that. You can do that with sass or node-sass.

10 hours ago, eydun said:

The idea is that the link to the css-file will change every second, and the browser will be tricked into thinking this is a new stylesheet and loading it fresh every time (and not cached copy).

Just for the sake of completion: This is horrible for a live site. You usually want to have something like this during development so you don't have to do a full reload every time, but on a live site this will prevent all your visitors from ever caching your site's CSS. This will have a bad impact on page speed, so make sure to remove that on the live site!

Link to comment
Share on other sites

15 minutes ago, MoritzLost said:

Just for the sake of completion: This is horrible for a live site. You usually want to have something like this during development so you don't have to do a full reload every time, but on a live site this will prevent all your visitors from ever caching your site's CSS. This will have a bad impact on page speed, so make sure to remove that on the live site!

That. The main.css under site/templates/assets/css is meant as a convenient place to add custom styling (no sass/less magic involved here). I had meant to change that time() call to an mtime() call as an example of how to bust the cache whenver the file is changed, but I apparently never got around to it. Brushing up my site templates is another point on my growing todo list.

The default styling is done in site/templates/assets/css/blog.css, but you won't see that filename in the developer console because the site template uses the AOIM module to combinde and minimize the js and css files.

  • Like 1
Link to comment
Share on other sites

1 hour ago, MoritzLost said:

You usually want to have something like this during development so you don't have to do a full reload every time

Nowadays open dev tools and disable the cache in there. No need for hacky cache busting on each request.

  • Like 1
Link to comment
Share on other sites

1 hour ago, LostKobrakai said:

Sure, but this is imo best done by appending e.g. the md5 hash of the file contents for cache busting. This one will actually only change if the css file itself changed.

That makes a lot of sense. 

Link to comment
Share on other sites

5 hours ago, BitPoet said:

That. The main.css under site/templates/assets/css is meant as a convenient place to add custom styling (no sass/less magic involved here). I had meant to change that time() call to an mtime() call as an example of how to bust the cache whenver the file is changed, but I apparently never got around to it. Brushing up my site templates is another point on my growing todo list.

The default styling is done in site/templates/assets/css/blog.css, but you won't see that filename in the developer console because the site template uses the AOIM module to combinde and minimize the js and css files.

Thank you BitPoet!

I hadn't thought of that. ?

Link to comment
Share on other sites

1 hour ago, LostKobrakai said:

Sure, but this is imo best done by appending e.g. the md5 hash of the file contents for cache busting. This one will actually only change if the css file itself changed.

As an alternative to this approach, my new Cache Control module also includes an optional feature for browser cache busting. You retrieve a version string (by default, a timestamp) through the module that is stored inside the cache. As soon as you clear the cache through the module, this version string gets cleared as well, and a new version string is generated during the next request. This ensures that any assets cached by the browser are cleared alongside all server-side caches. Just throwing it out there ?

  • Like 2
Link to comment
Share on other sites

2 hours ago, LostKobrakai said:

Sure, but this is imo best done by appending e.g. the md5 hash of the file contents for cache busting. This one will actually only change if the css file itself changed.

Doesn't that need to generate the md5 hash of the file on every request (if not cached)? I guess filemtime() is a lot faster than md5_file()?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...