Skip to content

Fix missing badge for Turbo Drive page loads #631

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Next Next commit
Fix missing badge for Turbo Drive page loads
Git bisect pointed to turbo-rails repo commit d18b2ee being the cause
of this bug. Previously Turbo Drive requests were displayed via the callback
added to the `window.fetch`. That turbo-rails commit however change it
so that a reference to `window.fetch` was stored when Turbo JS is loaded (before
rack-mini-profiler has added the then callback) meaning that the
callback to display those profiling request results never executes.

Adding a callback to `window.Turbo.fetch` is difficult as `window.Turbo`
is a frozen object, so instead a turbo event listener is used.
  • Loading branch information
coalest committed Jan 31, 2025
commit a40890d2c452bb6626564f98aed5ae54c976e2b4
94 changes: 53 additions & 41 deletions lib/html/includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,50 @@ var _MiniProfiler = (function() {
}
};

var fetchResultsFromAsyncResponse = function fetchResultsFromAsyncResponse(response) {
try {
// look for x-mini-profile-ids
var entries = response.headers.entries();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (
var _iterator = entries[Symbol.iterator](), _step;
!(_iteratorNormalCompletion = (_step = _iterator.next())
.done);
_iteratorNormalCompletion = true
) {
var pair = _step.value;

if (
pair[0] &&
pair[0].toLowerCase() == "x-miniprofiler-ids"
) {
var ids = pair[1].split(",");
fetchResults(ids);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
} catch (e) {
console.error(e);
}
}

var toQueryString = function toQueryString(data, parentKey) {
var result = [];
for (var key in data) {
Expand Down Expand Up @@ -496,6 +540,11 @@ var _MiniProfiler = (function() {
}, 3000);
};

var onTurboBeforeFetchResponse = function onTurboBeforeFetchResponse(e) {
var response = e.detail.fetchResponse.response;
fetchResultsFromAsyncResponse(response);
}

var onTurboBeforeVisit = function onTurboBeforeVisit(e) {
if(!e.defaultPrevented) {
window.MiniProfilerContainer = document.querySelector('body > .profiler-results')
Expand Down Expand Up @@ -670,6 +719,7 @@ var _MiniProfiler = (function() {
if (options.hotwireTurboDriveSupport) {
document.addEventListener("turbo:before-visit", onTurboBeforeVisit)
document.addEventListener("turbo:load", onTurboLoad)
document.addEventListener("turbo:before-fetch-response", onTurboBeforeFetchResponse);
}
};

Expand All @@ -685,6 +735,7 @@ var _MiniProfiler = (function() {
);
document.removeEventListener("turbo:before-visit", onTurboBeforeVisit);
document.removeEventListener("turbo:load", onTurboLoad);
document.removeEventListener("turbo:before-fetch-response", onTurboBeforeFetchResponse);
};

var initFullView = function initFullView() {
Expand Down Expand Up @@ -963,48 +1014,9 @@ var _MiniProfiler = (function() {
var originalFetchRun = __originalFetch(input, init);

originalFetchRun.then(function(response) {
try {
// look for x-mini-profile-ids
var entries = response.headers.entries();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (
var _iterator = entries[Symbol.iterator](), _step;
!(_iteratorNormalCompletion = (_step = _iterator.next())
.done);
_iteratorNormalCompletion = true
) {
var pair = _step.value;

if (
pair[0] &&
pair[0].toLowerCase() == "x-miniprofiler-ids"
) {
var ids = pair[1].split(",");
fetchResults(ids);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
} catch (e) {
console.error(e);
}
fetchResultsFromAsyncResponse(response)
});

return originalFetchRun;
};
}
Expand Down