-
Notifications
You must be signed in to change notification settings - Fork 98
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 trailing slash #179
Fix trailing slash #179
Conversation
Fixes the issue where a site has a trailing slash in the home_url.
@@ -119,7 +119,7 @@ public function get_theme_color() { | |||
public function get_manifest() { | |||
$manifest = array( | |||
'name' => wp_kses_decode_entities( get_bloginfo( 'name' ) ), | |||
'start_url' => get_home_url(), | |||
'start_url' => user_trailingslashit( get_home_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.
Shouldn't this be trailingslashit()
?
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 have tested it with and without a trailing slash in the home url and as I understand user_trailingslashit documentation it will check if the permalink settings has a trailing slash.
"Conditionally adds a trailing slash if the permalink structure has a trailing slash, strips the trailing slash if not."
https://codex.wordpress.org/Function_Reference/user_trailingslashit
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.
What about instead just doing.
'start_url' => home_url( '/' ),
This is what core does to get the URL to the front page: https://github.com/WordPress/wordpress-develop/blob/a8a4c09f33356475f4a3f3a9f5bdacd0c25bcb42/src/wp-includes/link-template.php#L332-L333
What is the underlying issue being fixed? |
get_home_url() returns the home url without a trailing slash and if the site has a trailing slash it will result in the service worker having a wrong scope. |
Are you working with a subdirectory install? What are the specific URLs you are working with? |
Yes Sir, it's a multi site installation with subdirectory setup, where each language/country has urls like https://www.site.com/en/ and https://www.site.com/da/ |
The intended behavior is for each site on a multisite install to get its own service worker installed. You're saying that if you go to the English site then the scope is wrong. Is it |
If I do a lighthouse audit I can see the following issue:
There is no issues with the fix and each site will have its own service worker. |
Ok, so the issue is that the start URL needs to be trailing slashed to match the scope, right? In that case, |
Correct, but if any other site has a setup without a trailing slash in the home_url then there might be a issue for them eg. https://www.site.com. What user_trailingslashit ensures is that if the permalinks config is setup to have a trailing slash it keeps it otherwise it will remove it. |
But if the |
I have tried to look into the code for where the scope it getting set and it seems it in wp_print_service_workers and the home_url( '/', 'relative' ) call. So we could force set a trailing slash for both the start_url and the scope, but wouldn't it be better to use user_trailingslashit( get_home_url() ) in both places and let the users permalinks config handle that decision? |
@westonruter Is there anything you need me to do? |
@nuvoPoint Please share specifically how you have configured your site so I can reproduce your setup. You have multisite subdirectory install, but you have turned off trailing slashes? |
@nuvoPoint ☝️ |
Sorry, I did not see your last comment. On my localhost it's setup on NginX with nothing more than:
And in wp-config:
Our live servers have a bit different setup on NginX, but with the same results. Please let me know if your need anything else? |
@nuvoPoint See my comment about suggesting There is an open issue in the service worker spec regarding See also this related issue I just opened: #203 |
Forcing the start_url to have a slash with home_url( '/' ) will work in our setup and fully acceptable for me. But it will might not work for people who has made "extraordinary efforts" to remove the slash, which user_trailingslashit( get_home_url() ) should take care of. |
I think this is the best option until w3c/ServiceWorker#1272 is resolved. |
That's fine for us - thanks! |
@nuvoPoint I just updated it. Can you confirm the change works now for you? |
I can confirm that it works! |
Fixes the issue where a site has a trailing slash in the home_url.