Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 8b86d36

Browse files
mkawalecjeffbcross
authored andcommitted
perf($http): move xsrf cookie check to after cache check in $http
$http was previously checking cookies to find an xsrf-token prior to checking the cache. This caused a performance penalty of about 2ms, which can be very significant when loading hundreds of template instances on a page. Fixes #7717
1 parent 9b51067 commit 8b86d36

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/ng/http.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -674,14 +674,6 @@ function $HttpProvider() {
674674
config.headers = headers;
675675
config.method = uppercase(config.method);
676676

677-
var xsrfValue = urlIsSameOrigin(config.url)
678-
? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
679-
: undefined;
680-
if (xsrfValue) {
681-
headers[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
682-
}
683-
684-
685677
var serverRequest = function(config) {
686678
headers = config.headers;
687679
var reqData = transformData(config.data, headersGetter(headers), config.transformRequest);
@@ -957,8 +949,17 @@ function $HttpProvider() {
957949
}
958950
}
959951

960-
// if we won't have the response in cache, send the request to the backend
952+
953+
// if we won't have the response in cache, set the xsrf headers and
954+
// send the request to the backend
961955
if (isUndefined(cachedResp)) {
956+
var xsrfValue = urlIsSameOrigin(config.url)
957+
? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
958+
: undefined;
959+
if (xsrfValue) {
960+
reqHeaders[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
961+
}
962+
962963
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
963964
config.withCredentials, config.responseType);
964965
}

test/ng/httpSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,25 @@ describe('$http', function() {
860860

861861
$httpBackend.flush();
862862
}));
863+
864+
it('should check the cache before checking the XSRF cookie', inject(function($browser, $cacheFactory) {
865+
var testCache = $cacheFactory('testCache'),
866+
executionOrder = [];
867+
868+
spyOn($browser, 'cookies').andCallFake(function() {
869+
executionOrder.push('cookies');
870+
return {'XSRF-TOKEN':'foo'};
871+
});
872+
spyOn(testCache, 'get').andCallFake(function() {
873+
executionOrder.push('cache');
874+
});
875+
876+
$httpBackend.expect('GET', '/url', undefined).respond('');
877+
$http({url: '/url', method: 'GET', cache: testCache});
878+
$httpBackend.flush();
879+
880+
expect(executionOrder).toEqual(['cache', 'cookies']);
881+
}));
863882
});
864883

865884

0 commit comments

Comments
 (0)