-
Notifications
You must be signed in to change notification settings - Fork 685
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 Service Worker matching external top-level URLs #3191
Conversation
At the moment the Service Worker matches any external top-level path (e.g. https://any.external.domain.com/?query_params) as a home url, as `isHomeRoute` makes a mere string equality check against `url.pathname`. I stumbled upon these while trying to load the script for Zendesk Chat, whose URL has the form `https://v2.zopim.com/?APP_KEY_HASH`. This commit introduces an additional check to make sure that the URL origin matches the service worker origin, too, just as it would happen when passing a `RegExp` (bc of additional assumptions of workbox on the use of `RegExp`s that only make them match on local URLS)
@magento create issue from PR |
|
83549d2
to
efe28fe
Compare
@@ -83,7 +83,7 @@ export default function() { | |||
* and the cycle repeats. | |||
*/ | |||
registerRoute( | |||
({ url }) => isHTMLRoute(url), | |||
({ url }) => url.origin === self.location.origin && isHTMLRoute(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.
Is this needed? The SW only has access to routes under the scope, in this case, the domain that launched 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.
I don't know about the whole service worker implementation in pwa-studio, but from here it seems that workbox may intercept external resources.
And without this change the script was loading through the service worker. The network tab showing it with a gear icon, and the response being the index template.
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.
Is this needed? The SW only has access to routes under the scope, in this case, the domain that launched it.
@revanth0212 I think, based on the link provided, if you pass a regex it will only match against the sw domain, but in our case we are intercepting all urls in the callback function.
Additionally, based on the isHomeRoute
function it seems like ANY url without sub-paths ie www.google.com
or zopim.com
would return true
so the SW would return our index. And if there was a sub path that happened to match the store code like www.google.com/en_us
it would also return true and therefor our index file.
So if an external asset (like zen desk's js) is fetched from root, or if a file is requested from a path that is the first level that matches our store code, it will hit this sw route.
if (url.pathname === '/') {
return true;
}
// If store code is in the url, the home route will be url.com/view_code.
// A trailing / may or may not follow.
if (process.env.USE_STORE_CODE_IN_URL === 'true') {
return AVAILABLE_STORE_VIEWS.some(
({ code }) =>
url.pathname === `/${code}/` || url.pathname === `/${code}`
);
}
};```
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 a valid fix for an issue that we're lucky to have not encountered before. Thank you for your contribution!
I will approve it, but I do wonder if we need to apply a similar pattern to this route that also just uses a callback instead of regex.
Additionally, if you are able to test the new path that would be cool. If not, please let us know so we can write the test before this gets merged.
@sirugh i have no experience writing tests and it will be too long before I have proper time to learn |
No worries! I like to give people the opportunity but if it's not something you have time to do, we can absolutely handle that. I see you already gave it a shot which looks great :) Thanks again for your time - we will take it from here. |
QA Pass ✅ I am getting MFTF failures for |
Description
At the moment the Service Worker matches any external top-level path (e.g. https://any.external.domain.com/?query_params) as a home url, as
isHomeRoute
makes a mere string equality check againsturl.pathname
. This makes it return the index template for any such request.I stumbled upon these while trying to load the script for Zendesk Chat, whose URL has the form
https://v2.zopim.com/?APP_KEY_HASH
.This commit introduces an additional check to make sure that the URL origin matches the service worker origin, too, just as it would happen when passing a
RegExp
(bc of additional assumptions of workbox on the use ofRegExp
s that only make them match on local URLS)Related Issue
Acceptance
Verification Stakeholders
Verification Steps
fetch('https://v2.zopim.com/?foo',{mode: 'no-cors'}).then(data => console.log(data))
in the consoleSpecification
Resolved issues: