-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
DBW: add audit for Date.now() usage #707
Conversation
TIL |
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.
Looking good so far
category: 'JavaScript', | ||
name: 'no-datenow', | ||
description: 'Site does not use Date.now() in its own scripts', | ||
helpText: 'Consider using <a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance/now" target="_blank">performance.now()</a>, which as better precision than <code>Date.now()</code> and always increases at a constant rate, independent of the system clock.', |
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.
FWIW, this does seem like one of those tests people are going to be annoyed about if they need Date.now()
for an actual timestamp since the epoch or if performance.now()
overhead isn't worth it
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.
@brendankenny not annoyed, but if not Date.now(), what should be use if what you need is not performance? Maybe lighthouse should show recommendations besides just the warning/error?
|
||
afterPass(options) { | ||
return options.driver.evaluateAsync(`(${collectDateNowUsage.toString()}())`) | ||
.then(errors => { |
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.
maybe something like dateNowUses
to differentiate from the function below this handling actual errors?
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.
done
err.url = `${JSON.stringify(err)}`; | ||
prev.push(err); | ||
} | ||
return prev; |
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 you want to dedupe these so if a function calling Date.now()
is itself called many times you don't get a long long list of them?
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.
done
@@ -15,7 +15,10 @@ | |||
<summary>URLs</summary> | |||
{{#each this}} | |||
<div class="http-resource"> | |||
<span class="http-resource__url">{{this.url}}</span><span class="http-resource__protocol">({{this.protocol}})</span> | |||
<span class="http-resource__url">{{this.url}}</span> |
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.
Shouldn't overload url
here like this. Maybe just add a place for a more general text string that is styled the same?
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.
done
// Filter out Date.now() usage from scripts on other domains. | ||
const results = artifacts.DateNowUse.errors.reduce((prev, err) => { | ||
if (url.parse(err.url).host === pageHost) { | ||
err.url = `${JSON.stringify(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.
you shouldn't modify the artifact itself here (and should pick a more descriptive property name once the formatter is changed)
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.
done
Some idle thoughts while thinking about the next audit you (or whoever) write that uses this technique: it might be good to pull it up into It could also maybe be made more robust by throwing and catching errors but using the |
} catch (e) { | ||
if (e.stack) { | ||
const split = e.stack.split('\n'); | ||
const m = split[split.length - 1].match(/(https?:\/\/.*):(\d+):(\d+)/); |
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 tried out this date.now monkeypatch in the console, but it led to e.stack
as
"Error: __called Date.now()__
at Error (native)
at Function.Date.now (<anonymous>:4:13)
at <anonymous>:1:6"
The match() then returns null. I think you could make the regex testing here a bit more liberal.
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.
done with the ST API
throw Error('__called Date.now()__'); | ||
} catch (e) { | ||
if (e.stack) { | ||
const split = e.stack.split('\n'); |
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.
HOLD UP WAIT A MINUTE
https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces
⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️
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.
done
Well that looks useful! On Mon, Sep 26, 2016, 5:23 PM Paul Irish notifications@github.com wrote:
|
Moved to the ST API. Works great. PTAL. |
I am one of those annoyed people, who does use and need Date.now(). My App has portions that require action from the customer by a specific date/time. Can we at least add an option to the report to ignore that rule? |
you can create a custom configuration and don't include the audit & gatherer |
R: @paulirish @brendankenny @GoogleChrome/lighthouse