From 1e74c05ac6ff89ae9e8227f7a02573bfb3fbe34e Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 11 Feb 2013 11:06:41 -0800 Subject: [PATCH] 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 --- ...AphrontDefaultApplicationConfiguration.php | 1 + .../CelerityStaticResourceResponse.php | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/aphront/configuration/AphrontDefaultApplicationConfiguration.php b/src/aphront/configuration/AphrontDefaultApplicationConfiguration.php index d175a175ad..1981ee4428 100644 --- a/src/aphront/configuration/AphrontDefaultApplicationConfiguration.php +++ b/src/aphront/configuration/AphrontDefaultApplicationConfiguration.php @@ -122,6 +122,7 @@ class AphrontDefaultApplicationConfiguration protected function getResourceURIMapRules() { return array( '/res/' => array( + '(?:(?P[0-9]+)T/)?'. '(?Ppkg/)?'. '(?P[a-f0-9]{8})/'. '(?P.+\.(?:css|js|jpg|png|swf|gif))' diff --git a/src/infrastructure/celerity/CelerityStaticResourceResponse.php b/src/infrastructure/celerity/CelerityStaticResourceResponse.php index 7fbfd809ca..a90dcb498e 100644 --- a/src/infrastructure/celerity/CelerityStaticResourceResponse.php +++ b/src/infrastructure/celerity/CelerityStaticResourceResponse.php @@ -103,7 +103,7 @@ final class CelerityStaticResourceResponse { } private function renderResource(array $resource) { - $uri = PhabricatorEnv::getCDNURI($resource['uri']); + $uri = $this->getURI($resource); switch ($resource['type']) { case 'css': return phutil_tag( @@ -206,7 +206,7 @@ final class CelerityStaticResourceResponse { $this->resolveResources(); $resources = array(); foreach ($this->packaged as $resource) { - $resources[] = PhabricatorEnv::getCDNURI($resource['uri']); + $resources[] = $this->getURI($resource); } if ($resources) { $response['javelin_resources'] = $resources; @@ -215,4 +215,32 @@ final class CelerityStaticResourceResponse { 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); + } + }