Skip to content

Commit

Permalink
Auto-update History panel with JavaScript fetch requests.
Browse files Browse the repository at this point in the history
The fetch and XHR requests don't share internals in JavaScript. We should
automatically capture both sets of requests as they occur.
  • Loading branch information
tim-schilling committed Oct 21, 2022
1 parent f138780 commit 69ef753
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
30 changes: 24 additions & 6 deletions debug_toolbar/static/debug_toolbar/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ const djdt = {
document.getElementById("djDebug").dataset.sidebarUrl;
const slowjax = debounce(ajax, 200);

function handleAjaxResponse(storeId) {
storeId = encodeURIComponent(storeId);
const dest = `${sidebar_url}?store_id=${storeId}`;
slowjax(dest).then(function (data) {
replaceToolbarState(storeId, data);
});
}

// Patch XHR / traditional AJAX requests
const origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
this.addEventListener("load", function () {
Expand All @@ -270,16 +279,25 @@ const djdt = {
if (
this.getAllResponseHeaders().indexOf("djdt-store-id") >= 0
) {
let store_id = this.getResponseHeader("djdt-store-id");
store_id = encodeURIComponent(store_id);
const dest = `${sidebar_url}?store_id=${store_id}`;
slowjax(dest).then(function (data) {
replaceToolbarState(store_id, data);
});
handleAjaxResponse(this.getResponseHeader("djdt-store-id"));
}
});
origOpen.apply(this, arguments);
};

const origFetch = window.fetch
window.fetch = function(url, options) {
const promise = origFetch(url, options)
promise.then(function(response) {
if (response.headers.get("djdt-store-id") != null) {
handleAjaxResponse(response.headers.get("djdt-store-id"));
}
// Don't resolve the response via .json(). Instead
// continue to return it to allow the caller to consume as needed.
return response
})
return promise
}
},
cookie: {
get(key) {
Expand Down
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Change log
Pending
-------

* Auto-update History panel for JavaScript ``fetch`` requests.


3.7.0 (2022-09-25)
------------------

Expand Down

0 comments on commit 69ef753

Please sign in to comment.