-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WebProfiler] added support for window.fetch calls in ajax section #19576
10000 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
Conversation
@javiereguiluz Can you validate this one? |
@robfrawley Can you test it? |
Could anyone please test this one? |
stackElement.error = r.status < 200 || r.status >= 400; | ||
stackElement.statusCode = r.status; | ||
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna try this PR
Status: needs work |
Applied fixes for @stof `s comments. |
@@ -1,4 +1,4 @@ | |||
<script{% if csp_script_nonce is defined and csp_script_nonce %} nonce={{ csp_script_nonce }}{% endif %}>/*<![CDATA[*/ |
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.
This removal must be reverted. You are breaking the CSP compatibility
I will review again when the indentation changes are reverted. They make it very hard to see what actually changed (and if I ignored the whitespace changes, I cannot comment on the diff anymore, which is not convenient) |
oops how did that happen? |
@stof done, sorryy |
var promise = oldFetch.apply(null, arguments); | ||
|
||
var method = 'GET'; | ||
if(arguments[1].method !== undefined){ |
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.
@ivoba The init
config object is optional (Promise<Response> fetch(input[, init]);
) therefore this can throw an error. First check if the 2nd argument even exists arguments[1] && arguments[1].method !== undefined
.
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.
@tsm91 correct. i fixed this.
@@ -235,6 +235,47 @@ | |||
} | |||
|
|||
{% if excluded_ajax_paths is defined %} | |||
|
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
var oldFetch = window.fetch; | ||
window.fetch = function () { | ||
if (!arguments[0].match(new RegExp({{ excluded_ajax_paths|json_encode|raw }}))) { | ||
|
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
|
||
var method = 'GET'; | ||
|
||
if(arguments[1] && arguments[1].method !== undefined){ |
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.
missing space after if
and before {
Really looking forward to this fix/feature. Is there any way I can assist? |
var oldFetch = window.fetch; | ||
window.fetch = function () { | ||
if (!arguments[0].match(new RegExp({{ excluded_ajax_paths|json_encode|raw }}))) { | ||
var promise = oldFetch.apply(null, arguments); |
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.
this call must be done outside the URL profiling blacklist check (and the return at the end too). Otherwise, you break fetch entirely for URLs blacklisted by the AJAX profiling
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.
@stof fixed
added error handling on promise catch, moved fetch block out of XHR block, fixed method detect, matched url against excluded urls
if (window.fetch) { | ||
var oldFetch = window.fetch; | ||
window.fetch = function () { | ||
var promise = oldFetch.apply(null, arguments); |
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.
oldFetch.apply(window, arguments);
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.
even better: .apply(this, arguments)
, keeping the same context
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.
fixed using @stof 's approach
stackElement.profile = r.headers.get('x-debug-token'); | ||
stackElement.profilerUrl = r.headers.get('x-debug-token-link'); | ||
Sfjs.renderAjaxRequests(); | ||
}).catch(function(err) { |
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.
it's better to use the second argument of then
instead of using catch
: if an error is thrown by Sfjs.renderAjaxRequests
, the request would be tagged as erroneous whereas the error came from the code executed after that.
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.
fixed
}).catch(function(err) { | ||
stackElement.loading = false; | ||
stackElement.error = true; | ||
}); |
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.
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 comment
The 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.
if (window.fetch) { | ||
var oldFetch = window.fetch; | ||
window.fetch = function () { | ||
var promise = oldFetch.apply(null, arguments); |
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.
Works perfectly, no security error, even in cors
mode
Sfjs.renderAjaxRequests(); | ||
}).catch(function(err) { | ||
stackElement.loading = false; | ||
stackElement.error = true; |
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.
what about adding a type
so we could make distinction between ajax (xhr)
and fetch
in the toolbar. As they do not behave the same way, it may be useful, WDYT?
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
i added type to stackElement and to display
|
||
if (window.fetch) { | ||
var oldFetch = window.fetch; | ||
window.fetch = function () { |
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.
what about using fetch
signature (fetch(url, options):promise
) instead of relying on arguments? I think it's easier to read
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.
absolutly, fixed
@@ -235,6 +235,44 @@ | |||
} | |||
|
|||
{% if excluded_ajax_paths is defined %} | |||
|
|||
if (window.fetch) { |
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. 10000 p>
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 comment
The 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.
also as the profiler is a dev tool, how many people develop on outdated browsers?
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.
the polyfill defines a window.fetch.polyfill
property (set to true) to allow to identify itself.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
ok i see, i added a check on polyfill if (window.fetch && window.fetch.polyfill === undefined)
@ivoba Do you need help with the templates so you can finish this feature for 3.2? :) |
…g fetch signature instead of arguments
Thank you @ivoba. |
…lls in ajax section (ivoba) This PR was squashed before being merged into the 3.2-dev branch (closes symfony#19576). Discussion ---------- [WebProfiler] added support for window.fetch calls in ajax section | Q | A | ------------- | --- | Branch? | "master" | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#17444 | License | MIT | Doc PR | reference to the documentation PR, if any This adds support for window.fetch calls to the Ajax section of the WebProfiler toolbar. Credits to @tbopec for implementation :) Commits ------- b1b4d70 [WebProfiler] added support for window.fetch calls in ajax section
This adds support for window.fetch calls to the Ajax section of the WebProfiler toolbar.
Credits to @tbopec for implementation :)