-
Notifications
You must be signed in to change notification settings - Fork 671
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
Fix: Avoid analyzing /favicon.ico
multiple times
#430
Conversation
@@ -488,7 +488,11 @@ export class Connector implements IConnector { | |||
* * uses `favicon.ico` and the final url after redirects. | |||
*/ | |||
private async getFavicon(element?: AsyncHTMLElement) { | |||
const href = element ? element.getAttribute('href') : '/favicon.ico'; | |||
if (!element || !element.getAttribute('href')) { |
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.
There is also the case when href
is empty.
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.
Also, I don't think getFavicon
should be responsible of checking these things, and instead, and at this point:
all the checks should be made to determine if valid favicon
was specified or not.
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.
@alrra If a page doesn't have favicon in the root directory and /favicon.ico
returns 404. Do we still want to list all the warnings/errors under this url?
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.
Do we still want to list all the warnings/errors under this url?
@qzhou1607 Sorry, I don't quite understand the question. Can you be more specific, thanks!
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.
@alrra Like http://www.example.com/favicon.ico returns 404. In the output of running sonar against http://www.example.com, we listed:
http://www.example.com: 1 issues
...
http://www.example.com/favicon.ico: 1 issues
...
But http://www.example.com/favicon.ico doesn't exist, it looks a little weird to still check its response.
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.
But http://www.example.com/favicon.ico doesn't exist, it looks a little weird to still check its response.
Yes. However, do make sure that CDP
aligns with jsdom
.
f46456a
to
41df7f0
Compare
@alrra @sarvaje I pushed up some more changes. I've talked to you both about this issue and please let me know your thought about the current solution. Here is a summary: The cause of the duplicate messages : Changes in the current solution :
|
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.
@qzhou1607 on top of the comment I left, can you fix the conflicts?
const hasFaviconLinkTag = hasAttributeWithValue(data.element, 'link', 'rel', 'icon'); | ||
// If favicon is not present in the html, CDP will send a request to get favicon from the root directory. | ||
// We should set the tag to true in this case to avoid fetching favicon twice. | ||
const hasFaviconInRootDir = data.request.url.endsWith('/favicon.ico'); |
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 don't think this is right. I could very well have the following url https://sonarwhal.com/images/favicon.ico
and the favicon will not be in the root dir.
We should add a test for this case too.
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.
Then the name isn't appropriate.
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 was talking with Qing, and you are right, if it isn't in the root, hasFaviconLinkTag
should be true, she is going to change how to calculate hasFaviconInRootDir
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.
@molant I think your comment is correct. There is a scenario that /images/favicon.ico
is included in the html as the resource a img
instead of a link
tag in the head (rel
set as icon
). In this scenario we shouldn't set the flag to true since the favicon image isn't included in the page in an appropriate way. We should still go into getFavicon
trying to manually download the favicon.
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 will work on adding more tests too :)
41df7f0
to
fb2abc2
Compare
fb2abc2
to
f93550f
Compare
@molant @alrra I added some tests to make sure that there are no duplicate requests to favicon, but some of them are failing in It might be relevant to this issue reported. |
|
f93550f
to
70b972c
Compare
70b972c
to
2ce9c4a
Compare
I modify the code and use an approach suggested by @sarvaje before: Ignore automatic requests(and responses) from |
@@ -226,7 +226,7 @@ class JSDOMConnector implements IConnector { | |||
* * uses `favicon.ico` and the final url after redirects. | |||
*/ | |||
private async getFavicon(element?: HTMLElement) { | |||
const href = element ? element.getAttribute('href') : '/favicon.ico'; | |||
const href = (element && element.getAttribute('href')) || '/favicon.ico'; |
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.
Any reason to do the assignment in this way instead of the old way?
In terms of readability I prefer the old way.
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.
@sarvaje This is due to the scenario when element
exists but href
is an empty string. We still want to send out a request to /favicon.ico
in this case.
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.
Ahhh, ok!
private async getFavicon(element?: AsyncHTMLElement) { | ||
const href = element ? element.getAttribute('href') : '/favicon.ico'; | ||
private async getFavicon(element: AsyncHTMLElement) { | ||
const href = (element && element.getAttribute('href')) || '/favicon.ico'; |
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.
same as before
tests/lib/connectors/collect.ts
Outdated
|
||
await runTest(t, connectorBuilder, serverConfig); | ||
|
||
t.plan(2); |
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.
t.plan
is not necessary here. Take a look to this
It makes sense when you need to be sure that a block of code with t.is
(or another test) is run.
For example:
t.plan(2);
try {
await runTest(t, connectorBuilder, serverConfig);
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').callCount, 1);
t.is(t.context.sonar.emitAsync.withArgs('fetch::end').args[0][1].request.url, faviconInLinkElementDir);
}catch(e){}
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.
@sarvaje Done. Thanks for the information!
2ce9c4a
to
749e993
Compare
CDP
already sends request to/favicon.ico
automatically.So when forcing to load favicon, we shouldn't send request
to
favicon.ico
again.Fix #427