Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from kirchbergerknorr/v1.0.0
Browse files Browse the repository at this point in the history
Code Version 1.0.0
  • Loading branch information
Apke authored Jan 18, 2018
2 parents b4df14b + 4039c82 commit d898c19
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 1 deletion.
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.idea/

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
# Magento 1 - Url Cache Control

[will follow]
Magento 1.x Extension

## Overview

This module appends the modified timestamp to skin URLs, to automatically bust Browser Cache on changes.

## Requirements and setup

### Requirements

Tested with Magento 1.9.3.6

This extension can be installed using [Composer](https://getcomposer.org/doc/01-basic-usage.md)


## Details

This Module just add a unix-timestamp parameter to each skin-url which uses

````php
// skin
\Mage_Core_Model_Design_Package::getSkinUrl()

// merged css
\Mage_Core_Model_Design_Package::getMergedCssUrl()

// merged js
\Mage_Core_Model_Design_Package::getMergedJsUrl()
````

to get URLs like that example:

````html
<!-- merged css -->
<link rel="stylesheet" type="text/css" href="http://my-domain.com/media/css/80155168870b61c9ca5c888e4b01857c.css?1509972745">

<!-- vendor include of jQuery -->
<script type="text/javascript" src="http://my-domain.com/skin/frontend/my-package/my-design/js/vendor/jquery.min.js?1478112303"></script>

<!-- It will append it to every url which is a file in skin folder. So it also works for images -->
<link rel="icon" href="http://my-domain.com/skin/frontend/my-package/my-design/favicon.ico?1478112292" type="image/x-icon">
````

By default there is no need to update `.htaccess`.

### Configuration

enable Module

## Support

If you have any problems with this extension, please open an [issue](https://github.com/kirchbergerknorr/Magento1_UrlCacheControl/issues).

## Contribution

Any contribution is highly appreciated. The best way to contribute is to [open a pull request](https://help.github.com/articles/about-pull-requests/).

## Authors

Nick Dilssner [[email protected]](mailto:[email protected])

## License

[Open Software License (OSL 3.0)](http://opensource.org/licenses/osl-3.0.php)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2018 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Kirchbergerknorr_UrlCacheControl_Helper_Data extends Mage_Core_Helper_Abstract {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2018 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Kirchbergerknorr_UrlCacheControl_Model_Design_Package extends Mage_Core_Model_Design_Package
{
/**
* parameter _type
*/
const SKIN = 'skin';

/**
* add modified timestamp to url
*
* {@inheritdoc}
*/
public function getSkinUrl($file = null, array $params = array())
{
if (empty($params['_type'])) {
$params['_type'] = self::SKIN;
}

// <kk: - optimized with getFilename() (contains updateParamDefaults(), but without referenced params - that means all changes happens in parent, except 'type')
// and excluded $params['_default'], because updateParamDefaults() also handles that
$filename = $this->getFilename($file, $params);
// kk>

// <kk: - after copied collecting of real file-path is done and after parent processing, add last-modified timestamp as url parameter
return $this->addModifiedParam(
parent::getSkinUrl($file, $params),
$filename
);
// kk>
}

/**
* add modified timestamp to url
*
* {@inheritdoc}
*/
public function getMergedCssUrl($files)
{
$isSecure = Mage::app()->getRequest()->isSecure();
// <kk: - really complete different to JS? why is css more secure or complicated then js?
$mergerDir = $isSecure ? 'css_secure' : 'css';
// kk>
$targetDir = $this->_initMergerDir($mergerDir);
if (!$targetDir) {
return '';
}

// base hostname & port
$baseMediaUrl = Mage::getBaseUrl('media', $isSecure);
$hostname = parse_url($baseMediaUrl, PHP_URL_HOST);
$port = parse_url($baseMediaUrl, PHP_URL_PORT);
if (false === $port) {
$port = $isSecure ? 443 : 80;
}

// merge into target file
$targetFilename = md5(implode(',', $files) . "|{$hostname}|{$port}") . '.css';

// <kk: - after copied collecting of real file-path is done and after parent processing, add last-modified timestamp as url parameter
// all the other stuff will handle parent method
$filename = $targetDir . DS . $targetFilename;
return $this->addModifiedParam(
parent::getMergedCssUrl($files),
$filename
);
// kk>
}

/**
* add modified timestamp to url
*
* {@inheritdoc}
*/
public function getMergedJsUrl($files)
{
$targetFilename = md5(implode(',', $files)) . '.js';
$targetDir = $this->_initMergerDir('js');
if (!$targetDir) {
return '';
}

// <kk: - after copied collecting of real file-path is done and after parent processing, add last-modified timestamp as url parameter
$filename = $targetDir . DS . $targetFilename;
return $this->addModifiedParam(
parent::getMergedJsUrl($files),
$filename
);
// kk>
}

/**
* add modified timestamp of existing file to url
*
* @param string $url url to add
* @param string $filename path to file of url
*
* @return string url with modified timestamp of file, if exists
*/
protected function addModifiedParam($url, $filename)
{
// TODO: validate if filemtime is also usable with url and if this is not calling the web-server
// file_exists and filemtime uses the same cache, so it is better to validate instead of using @filemtime
if (file_exists($filename) && is_file($filename) && ($time = filemtime($filename)) !== false) {
return $url . ((strpos($url, '?') === false) ? '?' : '&' ) . $time;
}
return $url;
}
}
30 changes: 30 additions & 0 deletions app/code/community/Kirchbergerknorr/UrlCacheControl/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!--
/**
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2018 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->

<config>
<modules>
<Kirchbergerknorr_UrlCacheControl>
<version>1.0.0</version>
</Kirchbergerknorr_UrlCacheControl>
</modules>
<global>
<helpers>
<Kirchbergerknorr_UrlCacheControl>
<class>Kirchbergerknorr_UrlCacheControl_Helper</class>
</Kirchbergerknorr_UrlCacheControl>
</helpers>
<models>
<core>
<rewrite>
<design_package>Kirchbergerknorr_UrlCacheControl_Model_Design_Package</design_package>
</rewrite>
</core>
</models>
</global>
</config>
17 changes: 17 additions & 0 deletions app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* @author Nick Dilßner <[email protected]>
* @copyright Copyright (c) 2018 kirchbergerknorr GmbH (http://www.kirchbergerknorr.de)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->

<config>
<modules>
<Kirchbergerknorr_UrlCacheControl>
<active>true</active>
<codePool>community</codePool>
</Kirchbergerknorr_UrlCacheControl>
</modules>
</config>
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "kirchbergerknorr/magento1_url-cache-control",
"type": "magento-module",
"description": "Append last modified timestamp to skin url's to automatically burst cache on changes.",
"minimum-stability": "dev",
"authors": [
{"name": "Nick Dilßner", "email": "[email protected]"}
]
}
2 changes: 2 additions & 0 deletions modman
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app/code/community/Kirchbergerknorr/UrlCacheControl app/code/community/Kirchbergerknorr/UrlCacheControl
app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml app/etc/modules/Kirchbergerknorr_UrlCacheControl.xml

0 comments on commit d898c19

Please sign in to comment.