1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 06:42:42 +01:00

Add timestamps to development-mode static resource URIs

Summary:
When a developer changes CSS, it is normally sufficient to reload the page to get changes to show up, because browsers revalidate resources on reload.

However, if you reload the page and then an Ajax request adds new CSS to the page, this CSS does not trigger revalidation. The developer must currently clear their cache or re-run `scripts/celerity_mapper.php webroot`, to get this request to skip cache. We rarely use CSS over Ajax right now, so this hasn't cropped up much, but Conpherence does use this and clearing the resource is a big pain.

This seems to work fine normally, but I'm worried it might break some of the extra-celerity-resources stuff Facebook is doing.

Test Plan: In development mode, changed `conpherence/message-pane.css` and saw changes reflected on reload. Verified normal page loads do not cause additional HTTP requests. This change has no effect in production mode.

Reviewers: edward, vrana, btrahan

Reviewed By: vrana

CC: aran

Maniphest Tasks: T2428

Differential Revision: https://secure.phabricator.com/D4902
This commit is contained in:
epriestley 2013-02-11 11:06:41 -08:00
parent ca0d6aca10
commit 1e74c05ac6
2 changed files with 31 additions and 2 deletions

View file

@ -122,6 +122,7 @@ class AphrontDefaultApplicationConfiguration
protected function getResourceURIMapRules() { protected function getResourceURIMapRules() {
return array( return array(
'/res/' => array( '/res/' => array(
'(?:(?P<mtime>[0-9]+)T/)?'.
'(?P<package>pkg/)?'. '(?P<package>pkg/)?'.
'(?P<hash>[a-f0-9]{8})/'. '(?P<hash>[a-f0-9]{8})/'.
'(?P<path>.+\.(?:css|js|jpg|png|swf|gif))' '(?P<path>.+\.(?:css|js|jpg|png|swf|gif))'

View file

@ -103,7 +103,7 @@ final class CelerityStaticResourceResponse {
} }
private function renderResource(array $resource) { private function renderResource(array $resource) {
$uri = PhabricatorEnv::getCDNURI($resource['uri']); $uri = $this->getURI($resource);
switch ($resource['type']) { switch ($resource['type']) {
case 'css': case 'css':
return phutil_tag( return phutil_tag(
@ -206,7 +206,7 @@ final class CelerityStaticResourceResponse {
$this->resolveResources(); $this->resolveResources();
$resources = array(); $resources = array();
foreach ($this->packaged as $resource) { foreach ($this->packaged as $resource) {
$resources[] = PhabricatorEnv::getCDNURI($resource['uri']); $resources[] = $this->getURI($resource);
} }
if ($resources) { if ($resources) {
$response['javelin_resources'] = $resources; $response['javelin_resources'] = $resources;
@ -215,4 +215,32 @@ final class CelerityStaticResourceResponse {
return $response; return $response;
} }
private function getURI($resource) {
$uri = $resource['uri'];
// In developer mode, we dump file modification times into the URI. When a
// page is reloaded in the browser, any resources brought in by Ajax calls
// do not trigger revalidation, so without this it's very difficult to get
// changes to Ajaxed-in CSS to work (you must clear your cache or rerun
// the map script). In production, we can assume the map script gets run
// after changes, and safely skip this.
if (PhabricatorEnv::getEnvConfig('phabricator.developer-mode')) {
$root = dirname(phutil_get_library_root('phabricator')).'/webroot/';
if (isset($resource['disk'])) {
$mtime = (int)filemtime($root.$resource['disk']);
} else {
$mtime = 0;
foreach ($resource['symbols'] as $symbol) {
$map = CelerityResourceMap::getInstance();
$symbol_info = $map->lookupSymbolInformation($symbol);
$mtime = max($mtime, (int)filemtime($root.$symbol_info['disk']));
}
}
$uri = preg_replace('@^/res/@', '/res/'.$mtime.'T/', $uri);
}
return PhabricatorEnv::getCDNURI($uri);
}
} }