2

Every time Gulp changes the CSS or JS files it keep the output file name same as before. How will browser know that file has been changed and need to reload the css and JS files?

To resolve this problem, I am decide to use the following Gulp plugin:

https://github.com/sindresorhus/gulp-rev

it rename the assets file by appending content hash to filenames

unicorn.css => unicorn-098f6bcd.css

is there any way to include the assets file using php dynamically? Assets name will change every time when new content added.

4
  • simple solution , try appending a random variable to the css url ie ,unicorn.css?no_cache=<?php echo time();?>Not recommended , but a work around Commented Feb 12, 2014 at 5:53
  • Not a good idea. Browser will load the assets in every requests. Commented Feb 12, 2014 at 5:58
  • @aman yes agreed totally , I said work around Commented Feb 12, 2014 at 6:01
  • Why don't you just configure your server to use E-Tags or Last Modified? Commented Feb 12, 2014 at 6:23

2 Answers 2

1

instead renaming file, you can send finger print with it. if file changes, last modified changes too. so you can easily use file's last modified as finger print:

public function putStyle($fileName)
{
    $fp = filemtime($fileName); // finger print by file last modified
    $fileName .= '?' . $fp;
    echo '<link rel="stylesheet" href="' . $fileName . '"/>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good idea.However, I am trying to avoid query string because the CDN will see that GET parameter and think that its a dynamic request, so it will ask your servers for the file, which defeats the purpose of having a cached copy in the CDN.
0

I have found a way to include file dynamically. its fast (takes about 1.5 ms) and working perfectly.

PHP has a native function (glob) for searching files. According to PHP docs "The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells."

glob()

// css path would be the full physical path of your css directory
$cssPath = '/css/*.css';
$stylesheets = glob($cssPath, GLOB_NOSORT);

// link all stylesheets
<?php foreach($stylesheets as $stylesheet): ?>

     <link rel="stylesheet" href="css/<?=basename($stylesheet)?>">

<?php endforeach; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.