Skip to content

Commit e4df959

Browse files
author
epriestley
committedApr 8, 2012
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
1 parent f7b569e commit e4df959

16 files changed

+739
-130
lines changed
 

‎scripts/celerity_mapper.php

+50-14
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,43 @@
144144

145145
$root = Filesystem::resolvePath($argv[1]);
146146

147-
echo "Finding static resources...\n";
147+
$resource_hash = PhabricatorEnv::getEnvConfig('celerity.resource-hash');
148+
$runtime_map = array();
149+
150+
echo "Finding raw static resources...\n";
151+
$raw_files = id(new FileFinder($root))
152+
->withType('f')
153+
->withSuffix('png')
154+
->withSuffix('jpg')
155+
->withSuffix('gif')
156+
->withSuffix('swf')
157+
->withFollowSymlinks(true)
158+
->setGenerateChecksums(true)
159+
->find();
160+
161+
echo "Processing ".count($raw_files)." files";
162+
foreach ($raw_files as $path => $hash) {
163+
echo ".";
164+
$path = '/'.Filesystem::readablePath($path, $root);
165+
$type = CelerityResourceTransformer::getResourceType($path);
166+
167+
$hash = md5($hash.$path.$resource_hash);
168+
$uri = '/res/'.substr($hash, 0, 8).$path;
169+
170+
$runtime_map[$path] = array(
171+
'hash' => $hash,
172+
'uri' => $uri,
173+
'disk' => $path,
174+
'type' => $type,
175+
);
176+
}
177+
echo "\n";
178+
179+
$xformer = id(new CelerityResourceTransformer())
180+
->setMinify(false)
181+
->setRawResourceMap($runtime_map);
182+
183+
echo "Finding transformable static resources...\n";
148184
$files = id(new FileFinder($root))
149185
->withType('f')
150186
->withSuffix('js')
@@ -155,26 +191,31 @@
155191

156192
echo "Processing ".count($files)." files";
157193

158-
$resource_hash = PhabricatorEnv::getEnvConfig('celerity.resource-hash');
159-
160194
$file_map = array();
161-
foreach ($files as $path => $hash) {
195+
foreach ($files as $path => $raw_hash) {
162196
echo ".";
163-
$name = '/'.Filesystem::readablePath($path, $root);
164-
$file_map[$name] = array(
165-
'hash' => md5($hash.$name.$resource_hash),
197+
$path = '/'.Filesystem::readablePath($path, $root);
198+
$data = Filesystem::readFile($root.$path);
199+
200+
$data = $xformer->transformResource($path, $data);
201+
$hash = md5($data);
202+
$hash = md5($hash.$path.$resource_hash);
203+
204+
$file_map[$path] = array(
205+
'hash' => $hash,
166206
'disk' => $path,
167207
);
168208
}
169209
echo "\n";
170210

171-
$runtime_map = array();
172211
$resource_graph = array();
173212
$hash_map = array();
174213

175214
$parser = new PhutilDocblockParser();
176215
foreach ($file_map as $path => $info) {
177-
$data = Filesystem::readFile($info['disk']);
216+
$type = CelerityResourceTransformer::getResourceType($path);
217+
218+
$data = Filesystem::readFile($root.$info['disk']);
178219
$matches = array();
179220
$ok = preg_match('@/[*][*].*?[*]/@s', $data, $matches);
180221
if (!$ok) {
@@ -202,11 +243,6 @@
202243

203244
$provides = reset($provides);
204245

205-
$type = 'js';
206-
if (preg_match('/\.css$/', $path)) {
207-
$type = 'css';
208-
}
209-
210246
$uri = '/res/'.substr($info['hash'], 0, 8).$path;
211247

212248
$hash_map[$provides] = $info['hash'];

0 commit comments

Comments
 (0)
Failed to load comments.