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(service-worker): check that test page is in SW scope #6609

Merged
merged 3 commits into from
Nov 21, 2018
Merged

Conversation

brendankenny
Copy link
Member

part of #6395

add an additional check to the service-worker audit that the test page is within the scope of a SW.

I'll follow up with a second PR that tests that the start_url (if it exists) is within scope. Right now this is just pass/fail rawValue, but we'll probably need an audit explanation with the second PR.

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

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

do we know of any URLs that this PR actually changes any PWA determination? AFAIK there shouldn't be request that can be fetchedViaServiceWorker without a service worker controlling it, or is the goal of this really to just have more helpful stepping stones?

const matchingSW = versions.filter(v => v.status === 'activated')
.find(v => new URL(v.scriptURL).origin === origin);
// Ensure page is included in a SW's scope.
// See https://w3c.github.io/ServiceWorker/v1/#scope-match-algorithm
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was really hoping this would just use Use .startsWith 😆

const swOpts = [{
status: 'activated',
scriptURL: 'https://example.com/serviceworker/sw.js',
scopeURL: 'https://example.com/',
Copy link
Collaborator

Choose a reason for hiding this comment

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

this can happen? for some reason I thought sw could only control it's own directory

EDIT: MDN still thinks so too 🤔

There is frequent confusion surrounding the meaning and use of scope. Since a service worker can't have a scope broader than its own location, only use the scope option when you need a scope that is narrower than the default.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, good point. I went too wild with the possible combinations of states :)

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, actually I was wrong again. The service worker's directory is the "max scope" and it can only be narrowed by serviceWorker.register(), however if you serve the SW with a Service-Worker-Allowed header, you can change the max scope to something higher up. Google Maps does this, for instance.

Copy link
Member Author

Choose a reason for hiding this comment

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

added a comment to explain

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah OK, they should update some of those docs :)

https://www.w3.org/TR/service-workers-1/#service-worker-script-response was a pretty good example too if ya want to throw that example in there

@brendankenny
Copy link
Member Author

brendankenny commented Nov 20, 2018

is the goal of this really to just have more helpful stepping stones?

Yeah, stepping stones, I think, to help figure out why the SW isn't working (it shouldn't show passing when the offline check is failing specifically because the SW has the wrong scope). I imagine it will only fail when first starting to use a SW or someone makes a typo :)

// artifacts.URL.finalUrl so audit accounts for any redirects.
const versions = artifacts.ServiceWorker.versions;
const url = artifacts.URL.finalUrl;
const matchingSWVersions = versions.filter(v => v.status === 'activated')
Copy link
Member

Choose a reason for hiding this comment

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

do we want to consider 'activating' or even 'installed'/'installing' ?
I can't really tell if including those cases could give a false positive.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure. The gatherer runs in beforePass of the offline pass...if it's just installing I don't think it'll be ready to serve assets for that pass. I'm not even sure if this is a possible state...if you're installing and a navigation to about:blank and back occurs, does it keep installing?

Copy link
Member

Choose a reason for hiding this comment

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

k sg. since we're in beforePass of offline, this seems entirely reasonable.

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

Successfully merging this pull request may close these issues.

None yet

3 participants