Skip to content
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

Merged
merged 9 commits into from
Aug 28, 2020

Conversation

adrianaixba
Copy link
Collaborator

for #9683

exposing service worker url, and making it available on the service worker audit

@adrianaixba adrianaixba requested a review from a team as a code owner August 26, 2020 22:36
@adrianaixba adrianaixba requested review from Beytoven and removed request for a team August 26, 2020 22:36
.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();
Copy link
Member

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

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

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 */
Copy link
Member

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

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.

.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; }[]} */
Copy link
Collaborator

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 ...

Copy link
Collaborator

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

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;}>} */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/** @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()))
Copy link
Contributor

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.

Copy link
Member

@paulirish paulirish left a 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)
Copy link
Member

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:

return versions
.filter(v => v.status === 'activated')
.filter(v => new URL(v.scriptURL).origin === pageUrl.origin);

so perhaps ss and ssA/B ?

Copy link
Contributor

@Beytoven Beytoven left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants