-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WebProfiler] added support for window.fetch calls in ajax section #19576
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
added error handling on promise catch, moved fetch block out of XHR block, fixed method detect, matched url against excluded urls
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,18 +235,25 @@ | |
} | ||
|
||
{% if excluded_ajax_paths is defined %} | ||
if (window.XMLHttpRequest && XMLHttpRequest.prototype.addEventListener) { | ||
if (window.fetch) { | ||
var oldFetch = window.fetch; | ||
window.fetch = function () { | ||
var promise = oldFetch.apply(null, arguments); | ||
|
||
if (window.fetch) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we try to detect native fetch here ? If someone uses a fetch polyfill based on XHR (e.g. the polyfill maintained by github), we would register the request twice otherwise (at the fetch level and at the xhr level) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how would you detect this? just check if the browser supports fetch? this needs to happen before any ployfill is loaded, im not sure how to do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the polyfill defines a And people are expected to try their site in old browsers from time to time if they support them. Having your dev tools working to debug errors is a good idea there. The Symfony debug toolbar is meant to work in old browsers, even if it is less fancy (as it is embed in the page itself). The profiler does not support them, but you can always open it in a modern browser in parallel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok i see, i added a check on polyfill |
||
var oldFetch = window.fetch; | ||
window.fetch = function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. absolutly, fixed |
||
var promise = oldFetch.apply(null, arguments); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. even better: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed using @stof 's approach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works perfectly, no security error, even in |
||
if (!arguments[0].match(new RegExp({{ excluded_ajax_paths|json_encode|raw }}))) { | ||
var method = 'GET'; | ||
if (arguments[1] && arguments[1].method !== undefined) { | ||
method = arguments[1].method; | ||
} | ||
|
||
var stackElement = { | ||
loading: true, | ||
error: false, | ||
url: arguments[0], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to match the URL against the excluded ajax paths before adding it in the request stack |
||
method: arguments[1].method, | ||
method: method, | ||
start: new Date() | ||
}; | ||
|
||
requestStack.push(stackElement); | ||
promise.then(function (r) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the promise gets rejected ? The stack element should not stay in loading state in this case |
||
stackElement.duration = new Date() - stackElement.start; | ||
|
@@ -256,11 +263,17 @@ | |
stackElement.profile = r.headers.get('x-debug-token'); | ||
stackElement.profilerUrl = r.headers.get('x-debug-token-link'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this compatible with CORS restriction or will it log a security warning ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @romainneutron any ideas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm gonna try this PR |
||
Sfjs.renderAjaxRequests(); | ||
}).catch(function(err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's better to use the second argument of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
stackElement.loading = false; | ||
stackElement.error = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah was thinking about that too, but was bit afraid of the extra effort. i see what i can do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, the extra work is mainly about figuring out the display. Adding the type property is not hard (it should just be done in both place initializing a stackElement) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i added type to stackElement and to display |
||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about adding the error content as a property and display this error in the toolbar? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its a good idea, but maybe we can put this in another PR since its also not implemented for XHR, afaik, and it would need some more adjustments in how we render the table. |
||
Sfjs.renderAjaxRequests(); | ||
return promise; | ||
}; | ||
} | ||
} | ||
|
||
return promise; | ||
}; | ||
} | ||
if (window.XMLHttpRequest && XMLHttpRequest.prototype.addEventListener) { | ||
var proxied = XMLHttpRequest.prototype.open; | ||
|
||
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra blank line here