-
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
core: expose service worker url on service worker audit #11329
Conversation
.map(v => registrations.find(r => r.registrationId === v.registrationId)) | ||
.filter(/** @return {r is LH.Crdp.ServiceWorker.ServiceWorkerRegistration} */ r => !!r) | ||
.map(r => new URL(r.scopeURL).href); | ||
const serviceWorkerUrls = new Map(); |
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.
we've been slowly adopting a foosByBars convention for our map names.. i think it'd help here... like scriptUrlByScopeUrl
|
||
// Populate serviceWorkerUrls map with the scriptURLs and scopeUrls of matchingSWVersions and registrations | ||
matchingSWVersions.forEach(function(version) { | ||
const tempRegistration = registrations.find(r => r.registrationId === version.registrationId); |
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.
matchedRegistration
? this signal is just that we have a match... and thus can continue, yeah?
@@ -73,23 +73,31 @@ class ServiceWorker extends Audit { | |||
* @param {Array<LH.Crdp.ServiceWorker.ServiceWorkerVersion>} matchingSWVersions | |||
* @param {Array<LH.Crdp.ServiceWorker.ServiceWorkerRegistration>} registrations | |||
* @param {URL} pageUrl | |||
* @return {string|undefined} | |||
* @return {Array<string> | undefined} | |||
*/ | |||
static getControllingScopeUrl(matchingSWVersions, registrations, pageUrl) { |
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.
now that this returns more than just the scope url we should rename it... perhaps getControllingServiceWorker
.filter(scopeUrl => pageUrl.href.startsWith(scopeUrl)) | ||
.sort((scopeA, scopeB) => scopeA.length - scopeB.length) | ||
const pageControllingUrls = [...serviceWorkerUrls] | ||
/* converts map to array, to properly filer and sort according to scopeUrl */ |
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.
using a Map() is good, but right now it pretty quickly is thrown away and we turn it into the [['scope1', 'scriptA'],['scope2','scriptB']]
representation. So we don't get much value out of it.
and separately the current return value is an array of strings, but you kinda have to know which is which.. I'd feel better making it explicit.
i remember in an earlier version you had named properties. i think within the matchingScopeUrls
functional methods you ended up making a Array<{scopeUrl: string, scriptUrl: string}>
or something
@adrianaixba you were right and that was better. :) i misled you as i thought doing that wasn't necessary. mea cupla.
so yeah looking at it now.. the best return value for this getControllingServiceWorker()
method would probably be {{scopeUrl: string, scriptUrl: string}}
, so we might as well build that from the start.
assert.deepStrictEqual(output, {score: 1}); | ||
assert.deepStrictEqual(output.score, 1); | ||
assert.deepStrictEqual(output.details.items[0].scopeURL, 'https://example.com/project/'); | ||
assert.deepStrictEqual(output.details.items[0].scriptURL, 'https://example.com/project/sw.js'); |
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 is satisfying. and exactly what the test should have been asserting beforehand anyway. without this assertion (/project/sw
is the controlling sw), the test was kinda useless.
…erviceWorker more clear
.map(v => registrations.find(r => r.registrationId === v.registrationId)) | ||
.filter(/** @return {r is LH.Crdp.ServiceWorker.ServiceWorkerRegistration} */ r => !!r) | ||
.map(r => new URL(r.scopeURL).href); | ||
/** @type {{ scopeUrl: string; scriptUrl: string; }[]} */ |
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.
nit: prefer Array<...>
, especially for complex types
there's a bit of an unofficial war between me, @patrickhulce and @brendankenny about when to use Array<...>
vs ...[]
:) I prefer using []
for simple, non-compound types like string[]
or LH.Artifact.Blah[]
, but others prefer always using it. eventually you may pick a side ...
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.
for the record I also have absolutely no problem with string[]
or number[]
:)
I think we all agree when the types grow to be more complex (I'd probably switch earlier than @connorjclark and use Array<LH.Nested.Thing>
) the Array<T>
form is easier to read 👍
const scriptByScopeUrlList = []; | ||
|
||
// Populate serviceWorkerUrls map with the scriptURLs and scopeUrls of matchingSWVersions and registrations | ||
matchingSWVersions.forEach(function(version) { |
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.
nit: prefer for (const blah of blahs)
over blahs.forEach
.
.map(v => registrations.find(r => r.registrationId === v.registrationId)) | ||
.filter(/** @return {r is LH.Crdp.ServiceWorker.ServiceWorkerRegistration} */ r => !!r) | ||
.map(r => new URL(r.scopeURL).href); | ||
/** @type {Array<{ scopeUrl: string; scriptUrl: string;}>} */ |
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.
/** @type {Array<{ scopeUrl: string; scriptUrl: string;}>} */ | |
/** @type {Array<{scopeUrl: string; scriptUrl: string}>} */ |
.filter(scopeUrl => pageUrl.href.startsWith(scopeUrl)) | ||
.sort((scopeA, scopeB) => scopeA.length - scopeB.length) | ||
const pageControllingUrls = scriptByScopeUrlList | ||
.filter(urlPair => pageUrl.href.startsWith(urlPair.scopeUrl.toString())) |
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.
Nit: I believe urlPair.scopeUrl
is already of type string
so the .toString()
isn't needed.
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.
another driveby from me. i'm the worst.
edit: ummmmmmmmmmmmmm i wrote a mediumsize comment but github lost it? :/
edit edit: shrug. ah well. i rewrote it.
.sort((scopeA, scopeB) => scopeA.length - scopeB.length) | ||
const pageControllingUrls = scriptByScopeUrlList | ||
.filter(urlPair => pageUrl.href.startsWith(urlPair.scopeUrl.toString())) | ||
.sort((scopeA, scopeB) => scopeA.scopeUrl.length - scopeB.scopeUrl.length) |
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 nit: we were calling this urlPair
when it was an array of strings, but now its a typed object... so the identity and type is pretty clear.
also we call this obj urlPair
here on 98 but then scopeA/B
on 99. we should keep it consistent.
in these sorts of cases we often go for short/singleletter var names... like this:
lighthouse/lighthouse-core/audits/service-worker.js
Lines 65 to 67 in 960d7c7
return versions | |
.filter(v => v.status === 'activated') | |
.filter(v => new URL(v.scriptURL).origin === pageUrl.origin); |
so perhaps ss
and ssA/B
?
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.
LGTM!
for #9683
exposing service worker url, and making it available on the service worker audit