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

Auto-update History panel with JavaScript fetch requests. #1685

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions debug_toolbar/static/debug_toolbar/js/toolbar.js
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 () {
const promise = origFetch.apply(this, arguments);
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
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
20 changes: 16 additions & 4 deletions example/templates/index.html
Expand Up @@ -18,18 +18,30 @@ <h1>Index of Tests</h1>
<p>
<span>Value </span>
<span id="session-value">{{ request.session.value|default:0 }}</span>
<button id="increment" data-url="{% url 'ajax_increment' %}" type="button">Increment</button>
<button id="incrementFetch" data-url="{% url 'ajax_increment' %}" type="button">Increment via fetch</button>
<button id="incrementXHR" data-url="{% url 'ajax_increment' %}" type="button">Increment via XHR</button>
</p>
<script>
const increment = document.querySelector("#increment");
const incrementFetch = document.querySelector("#incrementFetch");
const incrementXHR = document.querySelector("#incrementXHR");
const value = document.querySelector("#session-value");
increment.addEventListener("click", function () {
fetch(increment.dataset.url).then( function (response) {
incrementFetch.addEventListener("click", function () {
fetch(incrementFetch.dataset.url).then( function (response) {
response.json().then(function(data) {
value.innerHTML = data.value;
});
});
});
incrementXHR.addEventListener("click", function () {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
value.innerHTML = JSON.parse(xhr.response).value;
}
}
xhr.open('GET', incrementXHR.dataset.url, true);
xhr.send('');
});
</script>
</body>
</html>