Wp-rocket: CSS/JS minification/combination not working on Kinsta when the site's URL contains a path

Created on 17 Sep 2020  Â·  5Comments  Â·  Source: wp-media/wp-rocket

Before submitting an issue please check that you’ve completed the following steps:

  • Made sure you’re on the latest version ✅
  • Used the search feature to ensure that the bug hasn’t been reported before ✅

Describe the bug

The following was found on a multisite where the subsites were in subfolders. I haven't checked it, but the same should stand for WordPress subfolder installations.

On Kinsta, who do their own CDN rewriting, CSS/JavaScript combination fails when the site's URL contains a path, e.g. https://www.example.com/path/.

The following check is failing:
https://github.com/wp-media/wp-rocket/blob/8f7f67b621e5085ccf6424a08a15ce636e4fbf2f/inc/functions/formatting.php#L512

This is because in the following line:
https://github.com/wp-media/wp-rocket/blob/8f7f67b621e5085ccf6424a08a15ce636e4fbf2f/inc/Engine/CDN/Subscriber.php#L238

we are using only the host:

$site_url = $site_url_parts['scheme'] . '://' . $site_url_parts['host'];

Eventually the $file doesn't contain the root directory path here:
https://github.com/wp-media/wp-rocket/blob/8f7f67b621e5085ccf6424a08a15ce636e4fbf2f/inc/functions/formatting.php#L497

This returns false and assets aren't minified/combined.

To Reproduce

Steps to reproduce the behavior:

  1. Create a multisite and add a subfolder subsite.
  2. Add some dummy links to assets to emulate Kinsta's rewriting.
  3. Enable CDN and use the CNAME you used in the dummy links.
  4. Enable CSS/JavaScript minification/combination.
  5. Check the site to see if the minification/combination is working.

Expected behavior

CSS/JavaScript combination should work regardless of the site's URL containing a path when using Kinsta or any host that does CDN rewriting.

Additional context

Related ticket: https://secure.helpscout.net/conversation/1272259848/191944?folderId=2135277

Backlog Grooming (for WP Media dev team use only)

  • [ ] Reproduce the problem
  • [ ] Identify the root cause
  • [ ] Scope a solution
  • [ ] Estimate the effort
3rd party compatibility combine CSS combine JS minify CSS minify JS grooming medium major bug

Most helpful comment

props to @alfonso100

we can solve this issue by replacing the following line

https://github.com/wp-media/wp-rocket/blob/28e7b00cb74df0a18b79c65631d0ba438e019db8/inc/Engine/CDN/Subscriber.php#L231

with the following line

$site_url = untrailingslashit( $site_url_parts['scheme'] . '://' . $site_url_parts['host'] . $site_url_parts['path'] );

it should do the trick, I tried that on one of our customer's sites and it seems working.

All 5 comments

Any updates on this? It's quite a bad bug :/

The customer opened a new ticket about this case:
https://secure.helpscout.net/conversation/1332879329/210266?folderId=2683093

props to @alfonso100

we can solve this issue by replacing the following line

https://github.com/wp-media/wp-rocket/blob/28e7b00cb74df0a18b79c65631d0ba438e019db8/inc/Engine/CDN/Subscriber.php#L231

with the following line

$site_url = untrailingslashit( $site_url_parts['scheme'] . '://' . $site_url_parts['host'] . $site_url_parts['path'] );

it should do the trick, I tried that on one of our customer's sites and it seems working.

additional context till finishing grooming:
Kinsta ( and I think BunnyCDN ) when using CDN it removes the subdirectory path from the assets urls so when replacing the site url with the content path it gives not correct path so what we did here is to add the path also to the site url

for example:
main url: https://example.org/sub/
asset url without cdn: https://example.org/sub/wp-content/themes/test/style.css
asset url: https://mk0runnerslab22n01ig.kinstacdn.com/wp-content/themes/test/style.css
so we try to replace https://mk0runnerslab22n01ig.kinstacdn.com/ with https://example.org/ assuming that the asset url has the subdirectory on it (but at kinsta it isn't) so the final url is: https://example.org/wp-content/themes/test/style.css and this is not correct.

Related ticket: https://secure.helpscout.net/conversation/1367510564/223237/

@engahmeds3ed have already confirmed the fix there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webtrainingwheels picture webtrainingwheels  Â·  5Comments

Tabrisrp picture Tabrisrp  Â·  4Comments

vmanthos picture vmanthos  Â·  5Comments

Screenfeed picture Screenfeed  Â·  5Comments

engahmeds3ed picture engahmeds3ed  Â·  4Comments