Skip to content

Commit

Permalink
bar.js: fixed manual refreshing via 'X-Tracy-Ajax': Tracy.getAjaxHead…
Browse files Browse the repository at this point in the history
…er()

(works only with fetch mode)
  • Loading branch information
dg committed Dec 20, 2021
1 parent 14a4225 commit 59a3ae8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions examples/ajax-fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
// 'X-Tracy-Ajax': Tracy.getAjaxHeader()}, // use when auto-refresh is disabled via window.TracyAutoRefresh = false;
'X-Requested-With': 'XMLHttpRequest',
//'X-Tracy-Ajax': Tracy.getAjaxHeader(), // use when auto-refresh is disabled via window.TracyAutoRefresh = false;
}
}
)
Expand Down
3 changes: 1 addition & 2 deletions examples/ajax-jquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
jqxhr = $.ajax({
data: {error: $(this).hasClass('error') * 1},
dataType: 'json',
jsonp: false,
// headers: {'X-Tracy-Ajax': Tracy.getAjaxHeader()}, // use when auto-refresh is disabled via window.TracyAutoRefresh = false;
jsonp: false
}).done(function(data) {
$('#result').text('loaded: ' + data);

Expand Down
28 changes: 14 additions & 14 deletions src/Tracy/Bar/assets/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,16 @@ class Debug


static captureAjax() {
let header = Tracy.getAjaxHeader();
if (!header) {
if (!contentId) {
return;
}
let oldOpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function() {
oldOpen.apply(this, arguments);

if (autoRefresh && new URL(arguments[1], location.origin).host === location.host) {
let reqId = header + '_' + ajaxCounter++;
let reqId = Tracy.getAjaxHeader();
this.setRequestHeader('X-Tracy-Ajax', reqId);
this.addEventListener('load', function() {
if (this.getAllResponseHeaders().match(/^X-Tracy-Ajax: 1/mi)) {
Expand All @@ -488,20 +488,20 @@ class Debug
let oldFetch = window.fetch;
window.fetch = function(request, options) {
request = request instanceof Request ? request : new Request(request, options || {});
let reqId = request.headers.get('X-Tracy-Ajax');

if (autoRefresh && new URL(request.url, location.origin).host === location.host) {
let reqId = header + '_' + ajaxCounter++;
if (autoRefresh && !reqId && new URL(request.url, location.origin).host === location.host) {
reqId = Tracy.getAjaxHeader();
request.headers.set('X-Tracy-Ajax', reqId);
return oldFetch(request).then((response) => {
if (response instanceof Response && response.headers.has('X-Tracy-Ajax') && response.headers.get('X-Tracy-Ajax')[0] === '1') {
Debug.loadScript(baseUrl + '_tracy_bar=content-ajax.' + reqId + '&XDEBUG_SESSION_STOP=1&v=' + Math.random());
}

return response;
});
}

return oldFetch(request);
return oldFetch(request).then((response) => {
if (response instanceof Response && response.headers.has('X-Tracy-Ajax') && response.headers.get('X-Tracy-Ajax')[0] === '1') {
Debug.loadScript(baseUrl + '_tracy_bar=content-ajax.' + reqId + '&XDEBUG_SESSION_STOP=1&v=' + Math.random());
}

return response;
});
};
}

Expand Down Expand Up @@ -686,7 +686,7 @@ let Tracy = window.Tracy = window.Tracy || {};
Tracy.DebugPanel = Panel;
Tracy.DebugBar = Bar;
Tracy.Debug = Debug;
Tracy.getAjaxHeader = () => contentId;
Tracy.getAjaxHeader = () => contentId + '_' + ajaxCounter++;

Debug.setOptions({
panelZIndex: Tracy.panelZIndex,
Expand Down

0 comments on commit 59a3ae8

Please sign in to comment.