8000 [WebProfiler] added support for window.fetch calls in ajax section by ivoba · Pull Request #19576 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 4 commits into from

Conversation

ivoba
Copy link
Contributor
@ivoba ivoba commented Aug 9, 2016
Q A
Branch? "master"
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #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 :)

@fabpot
Copy link
Member
fabpot commented Aug 15, 2016

@javiereguiluz Can you validate this one?

@fabpot
Copy link
Member
fabpot commented Aug 15, 2016

@robfrawley Can you test it?

@nicolas-grekas
Copy link
Member

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');
Copy link
Member

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 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romainneutron any ideas?

Copy link
Contributor

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

@stof
Copy link
Member
stof commented Aug 23, 2016

Status: needs work

@ivoba
Copy link
Contributor Author
ivoba commented Aug 24, 2016

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[*/
Copy link
Member
@stof stof Aug 24, 2016

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

@stof
Copy link
Member
stof commented Aug 24, 2016

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)

@ivoba
Copy link
Contributor Author
ivoba commented Aug 24, 2016

oops how did that happen?
fix is coming...

@ivoba
Copy link
Contributor Author
ivoba commented Aug 24, 2016

@stof done, sorryy

var promise = oldFetch.apply(null, arguments);

var method = 'GET';
if(arguments[1].method !== undefined){
Copy link

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 .

Copy link
Contributor Author

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 %}

Copy link
Member
@fabpot fabpot Sep 14, 2016

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 }}))) {

Copy link
Member

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){
Copy link
Member

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 {

@RolfBabijn
Copy link

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);
Copy link
Member

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

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oldFetch.apply(window, arguments);

Copy link
Member
@stof stof Sep 30, 2016

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

Copy link
Contributor Author

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) {
Copy link
Contributor
@romainneutron romainneutron Sep 30, 2016

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.

Copy link
Contributor Author

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;
});
Copy link
Contributor

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?

Copy link
Contributor Author

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);
Copy link
Contributor
@romainneutron romainneutron Sep 30, 2016

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;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Member

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)

Copy link
Contributor Author

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 () {
Copy link
Contributor

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

Copy link
Contributor Author

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) {
Copy link
Member

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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)

@HeahDude
Copy link
Contributor
HeahDude commented Oct 1, 2016

@ivoba Do you need help with the templates so you can finish this feature for 3.2? :)

@fabpot
Copy link
Member
fabpot commented Oct 5, 2016

Thank you @ivoba.

@fabpot fabpot closed this in 2101c10 Oct 5, 2016
@fabpot fabpot mentioned this pull request Oct 27, 2016
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants
0