Browse files

Use the Date header to get server-side date

Before, we were passing the current server timestamp in the JSON.
But when the JSON is cached and a client is a little bit behind, they
may get a stale timestamp, which is awful.

Instead, we will be getting the server timestamp via the Date header,
which is required by all servers with clocks. See
http://pretty-rfc.herokuapp.com/RFC2616#header.date
  • Loading branch information...
1 parent e5e6138 commit bd6c841dd76da51ccc630e1dcba2ec84a08b57c2 @nb nb committed Aug 21, 2012
Showing with 9 additions and 7 deletions.
  1. +7 −2 js/liveblog.js
  2. +2 −5 liveblog.php
View
9 js/liveblog.js
@@ -67,7 +67,7 @@ var liveblog = {};
liveblog.ajax_request( url, {}, liveblog.get_recent_entries_success, liveblog.get_recent_entries_error );
};
- liveblog.get_recent_entries_success = function( response ) {
+ liveblog.get_recent_entries_success = function( response, status, xhr ) {
liveblog.consecutive_failures_count = 0;
@@ -76,7 +76,7 @@ var liveblog = {};
if ( response && response.latest_timestamp )
liveblog.latest_entry_timestamp = response.latest_timestamp;
- liveblog.latest_response_server_timestamp = response.current_timestamp;
+ liveblog.latest_response_server_timestamp = liveblog.server_timestamp_from_xhr( xhr );
liveblog.latest_response_local_timestamp = liveblog.current_timestamp();
liveblog.display_entries( response.entries );
@@ -267,6 +267,11 @@ var liveblog = {};
return Math.floor( Date.now() / 1000 );
};
+ liveblog.server_timestamp_from_xhr = function(xhr) {
+ var timestamp_milliseconds = Date.parse( xhr.getResponseHeader( 'Date' ) );
+ return Math.floor( timestamp_milliseconds / 1000 );
+ };
+
// Initialize everything!
$( document ).ready( liveblog.init );
View
7 liveblog.php
@@ -194,7 +194,6 @@ public static function handle_request() {
public static function ajax_entries_between() {
// Set some defaults
- $current_timestamp = time();
$latest_timestamp = 0;
$entries_for_json = array();
@@ -207,15 +206,15 @@ public static function ajax_entries_between() {
}
// Do not cache if it's too soon
- if ( $end_timestamp > $current_timestamp )
+ if ( $end_timestamp > time() )
self::$do_not_cache_response = true;
+ error_log( 'diff ' + (time() - $end_timestamp ) );
// Get liveblog entries within the start and end boundaries
$entries = self::$entry_query->get_between_timestamps( $start_timestamp, $end_timestamp );
if ( empty( $entries ) ) {
self::json_return( array(
'entries' => array(),
- 'current_timestamp' => $current_timestamp,
'latest_timestamp' => null
) );
}
@@ -232,7 +231,6 @@ public static function ajax_entries_between() {
// Setup our data to return via JSON
$result_for_json = array(
'entries' => $entries_for_json,
- 'current_timestamp' => $current_timestamp,
'latest_timestamp' => $latest_timestamp,
);
@@ -386,7 +384,6 @@ public static function ajax_insert_entry() {
// weren't any entries in between.
self::json_return( array(
'entries' => array( $entry->for_json() ),
- 'current_timestamp' => time(),
'latest_timestamp' => null
) );
}

0 comments on commit bd6c841

Please sign in to comment.