r71764 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71763‎ | r71764 | r71765 >
Date:23:25, 26 August 2010
Author:catrope
Status:deferred
Tags:
Comment:
resourceloader: Implement support for 304/If-Modified-Since
* Set Last-Modified header in all responses
* Compare any If-Modified-Since header to the latest mtime of the requested modules and return a 304 if appropriate
* Set Cache-Control and Expires headers
** Introduce $wgResourceLoaderClientMaxage and $wgResourceLoaderServerMaxage
* Fix bug in mtime calculation for site JS
* Remove unneeded inclusion of Resources.php
Modified paths:
  • /branches/resourceloader/phase3/includes/DefaultSettings.php (modified) (history)
  • /branches/resourceloader/phase3/includes/ResourceLoader.php (modified) (history)
  • /branches/resourceloader/phase3/includes/ResourceLoaderModule.php (modified) (history)
  • /branches/resourceloader/phase3/load.php (modified) (history)

Diff [purge]

Index: branches/resourceloader/phase3/includes/ResourceLoader.php
@@ -220,6 +220,22 @@
221221 $missing[] = $name;
222222 }
223223 }
 224+
 225+ // Calculate the mtime of this request. We need this, 304 or no 304
 226+ $mtime = 1;
 227+ foreach ( $modules as $name ) {
 228+ $mtime = max( $mtime, self::getModule( $name )->getmtime() );
 229+ }
 230+ header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $mtime ) );
 231+
 232+ // Check if there's an If-Modified-Since header and respond with a 304 Not Modified if possible
 233+ $ims = $request->getHeader( 'If-Modified-Since' );
 234+ if ( $ims !== false && wfTimestamp( TS_UNIX, $ims ) == $mtime ) {
 235+ header( 'HTTP/1.0 304 Not Modified' );
 236+ header( 'Status: 304 Not Modified' );
 237+ return;
 238+ }
 239+
224240 // Use output buffering
225241 ob_start();
226242 // A list of registrations will be collected and appended to mediawiki script-only output
Index: branches/resourceloader/phase3/includes/DefaultSettings.php
@@ -1624,6 +1624,19 @@
16251625 */
16261626 $wgClockSkewFudge = 5;
16271627
 1628+/**
 1629+ * Maximum time in seconds to cache resources served by the resource loader on
 1630+ * the client side (e.g. in the browser cache).
 1631+ */
 1632+$wgResourceLoaderClientMaxage = 30*24*60*60; // 30 days
 1633+
 1634+/**
 1635+ * Maximum time in seconds to cache resources served by the resource loader on
 1636+ * the server side. This means Squid/Varnish but also any other public proxy
 1637+ * cache between the client and MediaWiki.
 1638+ */
 1639+$wgResourceLoaderServerMaxage = 60*60; // 1 hour
 1640+
16281641 /** @} */ # end of cache settings
16291642
16301643 /************************************************************************//**
Index: branches/resourceloader/phase3/includes/ResourceLoaderModule.php
@@ -365,7 +365,7 @@
366366 $retval = 1; // wfTimestamp() interprets 0 as "now"
367367 foreach ( $jsPages as $jsPage ) {
368368 if ( $jsPage->exists() ) {
369 - $retval = max( $retval, $jsPage->getTouched() );
 369+ $retval = max( $retval, wfTimestamp( TS_UNIX, $jsPage->getTouched() ) );
370370 }
371371 }
372372 return $retval;
Index: branches/resourceloader/phase3/load.php
@@ -25,8 +25,6 @@
2626 * This file is the entry point for the resource loader.
2727 */
2828
29 -// TODO: Caching + easy 304s before WebStart
30 -
3129 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
3230 wfProfileIn( 'load.php' );
3331
@@ -47,8 +45,12 @@
4846 // FIXME: Doesn't this execute the rest of the request anyway?
4947 // Was taken from api.php so I guess it's maybe OK but it doesn't look good.
5048 }
51 -// Include core resource list
52 -require_once "$IP/resources/Resources.php";
 49+
 50+// Set caching headers
 51+$expires = wfTimestamp( TS_RFC2822, min( $wgResourceLoaderClientMaxage, $wgResourceLoaderServerMaxage ) + time() );
 52+header( "Cache-Control: public, maxage=$wgResourceLoaderClientMaxage, s-maxage=$wgResourceLoaderServerMaxage" );
 53+header( "Expires: $expires" );
 54+
5355 // Respond to resource loading request
5456 ResourceLoader::respond( $wgRequest, $wgServer . $wgScriptPath . '/load.php' );
5557

Status & tagging log