Skip to content

Commit

Permalink
[FEATURE] Append timestamp to not merged css and js
Browse files Browse the repository at this point in the history
Now js and css files will have timestamp appended even if they are not
bundled into one file.

This change also adds timestamp to files included directly in the template
e.g.
<script type="text/javascript"
	src="<?php echo $this->getSkinUrl('js/jquery.js'); ?>"></script>
  • Loading branch information
tmotyl committed Sep 20, 2013
1 parent 7e1fbb6 commit 5471779
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion app/code/local/Aoe/JsCssTstamp/Model/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ protected function _prepareUrl($uri)
}

if ($this->addTstampToAssets) {
Mage::log('Aoe_JsCssTsamp: ' . $uri);
$matches = array();
if (preg_match('/(.*)\.(gif|png|jpg)$/i', $uri, $matches)) {
$uri = $matches[1] . '.' . $this->getVersionKey() . '.' . $matches[2];
Expand All @@ -316,4 +315,40 @@ protected function _prepareUrl($uri)

return $uri;
}

/**
* Get skin file url
*
* @param string $file
* @param array $params
* @return string
*/
public function getSkinUrl($file = null, array $params = array())
{
Varien_Profiler::start(__METHOD__);
if (empty($params['_type'])) {
$params['_type'] = 'skin';
}
if (empty($params['_default'])) {
$params['_default'] = false;
}
$this->updateParamDefaults($params);
if (!empty($file)) {
$result = $this->_fallback($file, $params, array(
array(),
array('_theme' => $this->getFallbackTheme()),
array('_theme' => self::DEFAULT_THEME),
));
}
$result = $this->getSkinBaseUrl($params) . (empty($file) ? '' : $file);
Varien_Profiler::stop(__METHOD__);

if ($this->addTstampToAssets) {
$matches = array();
if (preg_match('/(.*)\.(css|js)$/i', $result, $matches)) {
$result = $matches[1] . '.' . $this->getVersionKey() . '.' . $matches[2];
}
}
return $result;
}
}

10 comments on commit 5471779

@fbrnc
Copy link

@fbrnc fbrnc commented on 5471779 Mar 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tmotyl,

I've added this feature to the latest Aoe_JsCssTstamp version (and bumped it to 0.7.0).
I modified your implementation

  • I called parent::getSkinUrl() instead of copying the implementation
  • I introduced to new parameter so that css, js and the other assets can be controlled individually (it wasn't very intuitive that js files were versioned just because you enabled the addTstampToAsset option in the CSS section).

Thank you for this implementation. That helped me to save some time :)

Have a great day,
Fabrizio

@tmotyl
Copy link
Member Author

@tmotyl tmotyl commented on 5471779 Mar 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to hear :)
however nowadays most of the time we use https://github.com/GordonLesti/Lesti_Merge with some modifications as it allows to group js by handle.

@fbrnc
Copy link

@fbrnc fbrnc commented on 5471779 Mar 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint. I'll definitely check that out. Lest_Merge's approach to group files by handle seems to be smart :)

@fbrnc
Copy link

@fbrnc fbrnc commented on 5471779 Mar 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, just by reading his code I can't get how he actually create the different bundles. I can see the module is traversing the layout xml and will "remember" to origin of addJs/* methods by adding a new attribute to the xml.
But then there's no additional code that does something with this new information. Am I missing something?
And does this solve the problem of cache busting (by adding a dynamic portion to the filenames)?

@tmotyl
Copy link
Member Author

@tmotyl tmotyl commented on 5471779 Mar 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use the Magento feature to combine js, then the filename is a hash based on the filenames.
I don't remember right now if it adds the hash when combining is disabled.

We've improved the module, that it calculates the filename hash based on the file content (instead of filenames). And with this modification your're safe :)

@priyanka-saini
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After calling this parent::getSkinUrl(), the CSS and Js files are with timestamp. But when I open them they are showing not found. Can someone help in this?

@tmotyl
Copy link
Member Author

@tmotyl tmotyl commented on 5471779 Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@priyanka-saini make sure your webserver apache/nginx is configured correctly

@priyanka-saini
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I am using Filesystem not database. As per the documentation I should not do any thing in nginx

@tmotyl
Copy link
Member Author

@tmotyl tmotyl commented on 5471779 Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if not using database you still need rewrite rule like RewriteRule (.*)\.(\d{10})\.(gif|png|jpg)$ $1.$3 [L,NC]

@priyanka-saini
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I do not use it for images

Please sign in to comment.