2011-07-04 20:22:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
Use Celerity to version all static resources
Summary:
We don't use versioned URIs for images, so when they change users may get old versions.
This was a particular issue with the recent logo change, which several users reported cache-related issues from.
Instead, use Celerity to manage image URI versions in addition to CSS/JS.
This is complicated, because we need to rewrite image URIs inside of CSS, which means the hash of a CSS file has to be derived from the current image data. Otherwise, when we updated an image the CSS wouldn't update, so we wouldn't be any better off.
So basically we:
- Find all the "raw" files, and put them into the map.
- Find all the CSS/JS, perform content-altering transformations on it (i.e., not minification) based on the partial map, and then put it into the map based on transformed hashes.
(If we wanted, we could now do CSS variables or whatever for "free", more or less.)
Test Plan:
- Regenerated celerity map, browsed site, verified images generated with versioned URIs.
- Moved "blue" flag image over "green" flag image, regenerated map, verified "green" flag image and the associated CSS changed hashes.
- Added transformation unit tests; ran unit tests.
Reviewers: btrahan, vrana, jungejason
Reviewed By: vrana
CC: aran
Maniphest Tasks: T1073
Differential Revision: https://secure.phabricator.com/D2146
2012-04-08 19:07:51 +02:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-07-04 20:22:42 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include a CSS or JS static resource by name. This function records a
|
|
|
|
* dependency for the current page, so when a response is generated it can be
|
|
|
|
* included. You can call this method from any context, and it is recommended
|
|
|
|
* you invoke it as close to the actual dependency as possible so that page
|
|
|
|
* dependencies are minimized.
|
|
|
|
*
|
|
|
|
* For more information, see @{article:Adding New CSS and JS}.
|
|
|
|
*
|
|
|
|
* @param string Name of the celerity module to include. This is whatever you
|
|
|
|
* annotated as "@provides" in the file.
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @group celerity
|
|
|
|
*/
|
|
|
|
function require_celerity_resource($symbol) {
|
|
|
|
$response = CelerityAPI::getStaticResourceResponse();
|
|
|
|
$response->requireResource($symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a node ID which is guaranteed to be unique for the current page,
|
|
|
|
* even across Ajax requests. You should use this method to generate IDs for
|
|
|
|
* nodes which require a uniqueness guarantee.
|
|
|
|
*
|
|
|
|
* @return string A string appropriate for use as an 'id' attribute on a DOM
|
|
|
|
* node. It is guaranteed to be unique for the current page, even
|
|
|
|
* if the current request is a subsequent Ajax request.
|
|
|
|
*
|
|
|
|
* @group celerity
|
|
|
|
*/
|
|
|
|
function celerity_generate_unique_node_id() {
|
|
|
|
static $uniq = 0;
|
|
|
|
$response = CelerityAPI::getStaticResourceResponse();
|
|
|
|
$block = $response->getMetadataBlock();
|
|
|
|
|
|
|
|
return 'UQ'.$block.'_'.($uniq++);
|
|
|
|
}
|
|
|
|
|
Use Celerity to version all static resources
Summary:
We don't use versioned URIs for images, so when they change users may get old versions.
This was a particular issue with the recent logo change, which several users reported cache-related issues from.
Instead, use Celerity to manage image URI versions in addition to CSS/JS.
This is complicated, because we need to rewrite image URIs inside of CSS, which means the hash of a CSS file has to be derived from the current image data. Otherwise, when we updated an image the CSS wouldn't update, so we wouldn't be any better off.
So basically we:
- Find all the "raw" files, and put them into the map.
- Find all the CSS/JS, perform content-altering transformations on it (i.e., not minification) based on the partial map, and then put it into the map based on transformed hashes.
(If we wanted, we could now do CSS variables or whatever for "free", more or less.)
Test Plan:
- Regenerated celerity map, browsed site, verified images generated with versioned URIs.
- Moved "blue" flag image over "green" flag image, regenerated map, verified "green" flag image and the associated CSS changed hashes.
- Added transformation unit tests; ran unit tests.
Reviewers: btrahan, vrana, jungejason
Reviewed By: vrana
CC: aran
Maniphest Tasks: T1073
Differential Revision: https://secure.phabricator.com/D2146
2012-04-08 19:07:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the versioned URI for a raw resource, like an image.
|
|
|
|
*
|
|
|
|
* @param string Path to the raw image.
|
|
|
|
* @return string Versioned path to the image, if one is available.
|
|
|
|
*
|
|
|
|
* @group celerity
|
|
|
|
*/
|
|
|
|
function celerity_get_resource_uri($resource) {
|
|
|
|
$map = CelerityResourceMap::getInstance();
|
|
|
|
|
|
|
|
$info = $map->lookupFileInformation($resource);
|
|
|
|
if ($info) {
|
|
|
|
return $info['uri'];
|
|
|
|
} else {
|
|
|
|
return $resource;
|
|
|
|
}
|
|
|
|
}
|