Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REST API cache busting mechanism. #6948

Merged
merged 3 commits into from Apr 24, 2017
Merged

Conversation

zinigor
Copy link
Member

@zinigor zinigor commented Apr 7, 2017

Fixes #5217

Changes proposed in this Pull Request:

  • Refactored the REST API component to make it more DRY.
  • Adds a cache busting mechanism to address the problem of requests being cached.

Testing instructions:

  • I had a really hard time reproducing the problem on my own server, but it could be reproduced on SiteGround shared hosting. If you enable the browser console and allow browser caching in the settings, you will see that some requests come back with 304 Not Modified. My guess is that it's because the server sends back the Last-Modified header with the zero epoch time every time. If you can't reproduce it, try adding the header to API responses manually with this:
add_filter( 'rest_post_dispatch', function( $response, $server, $request ) {
	$response->set_headers(
		array_merge(
			$response->get_headers(),
			array(
				'Last-Modified' => date( 'r', 0 )
			)
		)
	);
	return $response;
}, 3, 10 );
  • See that the requests are cached.
  • Update to this PR.
  • See that the cache busting mechanism defeats the problem.

Proposed changelog entry for your changes:

  • Fixed the problem of API requests being cached on some hosts.

@zinigor zinigor added Admin Page React-powered dashboard under the Jetpack menu [Status] Needs Review To request a review from Crew. Label will be renamed soon. [Type] Bug When a feature is broken and / or not performing as intended labels Apr 7, 2017
}

function postRequest( route, body ) {
return fetch( addCacheBuster( route ), assign( {}, postParams, body ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary to cache bust POST requests. Browsers don't cache them by default.

}

function getRequest( route ) {
return fetch( addCacheBuster( route ), getParams );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to pass in the getParams explicitly into the function. So that the code is more readable.

: '',
args = query.split( '&' );

args.push( '_rnd=' + ( 10e9 * Math.random() ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing generating a random math character. I would suggest we just use the current time in seconds or milliseconds with

var ddate = new Date();
var milliseconds = d.getTime();

also can we make the _rnd more explicit with something like _cacheBuster

@dereksmart
Copy link
Member

I like enej's suggestions, but otherwise this is a nice change 👍

…he busting on POST requests, fixed query param handling.
@zinigor
Copy link
Member Author

zinigor commented Apr 13, 2017

Thanks for the review @enejb, @dereksmart ! I have made changes to account for all the comments.

@jeherve jeherve modified the milestone: 4.9 Apr 24, 2017
@dereksmart
Copy link
Member

Changes look good! Let's get this in

@dereksmart dereksmart added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from Crew. Label will be renamed soon. labels Apr 24, 2017
@eliorivero
Copy link
Contributor

Looks good!

@eliorivero eliorivero merged commit c307b0c into master Apr 24, 2017
@eliorivero eliorivero deleted the add/rest-api-cache-busting branch April 24, 2017 22:48
@eliorivero eliorivero removed the [Status] Ready to Merge Go ahead, you can push that green button! label Apr 24, 2017
jeherve added a commit that referenced this pull request Apr 24, 2017
eliorivero pushed a commit that referenced this pull request Apr 25, 2017
* Changelog: initial commit for 4.9 release.

* Changelog: add #6929

* Changelog: move old changelogs to changelog.txt

* Readme: restore deleted release post link.

The post is now live.

* Changelog: add #6853

* Changelog: add #6856

* Changelog: add #6857

* Changelog: add #6884

* Changelog: add #6885

* Changelog: add #6892

* Changelog: add #6894

* Changelog: add #6898

* Changelog: add #6899

* Changelog: add #6900

* Changelog: add #6909

* Changelog: add #6927

* Changelog: add #6947

* Chagelog: add #6958

* Changelog: add #6961

* Changelog: add #6963

* Changelog: add #6965

* Changelog: add #6986

* Changelog: add #7000

* Changelog: add #7013

* Changelog: add #7015

* Changelog: add #7019

* Changelog: add #7028

* Changelog: add #6998

* Changelog: add #6999

* Changelog: add #7044

* Changelog: add #6881

* Changelog: add #6922

* Changelog: add #6940

* Changelog: add #6962

* Changelog: add #6942

* Changelog: add #6959

* Changelog: add #7018

* Changelog: add #6948

* Changelog: add #6657

* Changelog: add #7030

* Changelog: add #7048

* Changelog: add #7031

* Changelog: add #6990

* Changelog: add #6957

* Changelog: add #7027
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Admin Page React-powered dashboard under the Jetpack menu [Pri] High [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants