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

Ensure service worker script src is same as scope #266

Merged
merged 1 commit into from
Mar 30, 2020

Conversation

westonruter
Copy link
Collaborator

@westonruter westonruter commented Mar 29, 2020

Fixes #231.

When testing the WPGlobus plugin I found that when accessing the Spanish-language version of the site at /es/ I was getting a service worker installed as:

navigator.serviceWorker.register(
	'https://example.com/es/?wp_service_worker=1',
	{ scope: '/'}
).then( reg => { /* ... */ });

Note the inconsistency in the scope of / and the script src having a base URL of /es/. This resulted in an error:

The path of the provided scope ('/') is not under the max scope allowed ('/es/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.

The issue is that the WPGlobus plugin is apparently filtering the home_url differently based on the scheme being supplied to home_url(). So the quick fix is just to make sure that the home_url() used to obtain the script URL be made consistent with the URL used for the scope.

$scopes[ WP_Service_Workers::SCOPE_FRONT ] = home_url( '/', 'relative' ); // The home_url() here will account for subdirectory installs.

return add_query_arg(
array( WP_Service_Workers::QUERY_VAR => $scope ),
home_url( '/' )
);

The issue is fixed merely by changing the latter from home_url( '/' ) to home_url( '/', 'relative ).

@westonruter westonruter added this to the 0.4 milestone Mar 29, 2020
@westonruter westonruter force-pushed the fix/service-worker-src-scope-consistency branch from 3838116 to e5fd349 Compare March 29, 2020 18:41
@westonruter
Copy link
Collaborator Author

Build for testing: pwa.zip (v0.4.0-alpha-e5fd349-20200329T184145Z)

Copy link
Collaborator

@amedina amedina left a comment

Choose a reason for hiding this comment

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

LGTM!

@westonruter westonruter merged commit 19ecf75 into master Mar 30, 2020
@westonruter westonruter deleted the fix/service-worker-src-scope-consistency branch March 30, 2020 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate compatibility with WPML, Polylang, etc
2 participants