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

Boost: Fix skip proxy origins to load css during critical CSS generation #20793

Merged
merged 8 commits into from
Aug 30, 2021

Conversation

haqadn
Copy link
Contributor

@haqadn haqadn commented Aug 23, 2021

While generating critical CSS, the CSS files need to be proxied through the plugin to avoid CORS restrictions. Previously the proxy was skipped if the server hostname matched the request, or if there was no hostname provided in the URL. Now, it has a filter hook to allow more control.

Correction: As @davidlonjon later pointed out, we don't really need the filter to make this work. The real issue is, the port was not taking into consideration while comparing hostname.

Changes proposed in this Pull Request:

Improved hostname comparison to take port into account.

Reason behind:

While developing locally through docker, the docker container internally would understand the hostname as localhost. However, we can also change the external port mapping through PORT_WORDPRESS env variable.
This leads to an issue. The external hostname and internal hostname are different so they are proxied while loading assets for critical CSS generation. Since the external URLs of the asset would contain the port in the request, it would be unreachable internally by the plugin.

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  • Specify a value for PORT_WORDPRESS in tools/docker/.env. The value should be something other than 80(as that would previously break). Read more.
  • Restart your jetpack development environment and make sure you can now access the the dev site at the new port i.e. localhost:
  • Try to regenerate CSS.
  • Critical CSS should now generate without any issue.

@haqadn haqadn requested a review from thingalon August 23, 2021 11:24
@github-actions github-actions bot added [Boost Feature] Critical CSS Issues involving the Critical CSS feature in Boost [Plugin] Boost A feature to speed up the site and improve performance. labels Aug 23, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Aug 23, 2021

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ⚠️ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Boost plugin:

  • Next scheduled release: September 7, 2021.
  • Scheduled code freeze: August 30, 2021.

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Aug 23, 2021
@haqadn haqadn added [Status] Needs Team Review and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Aug 23, 2021
@davidlonjon davidlonjon self-requested a review August 24, 2021 05:30
Copy link
Contributor

@davidlonjon davidlonjon left a comment

Choose a reason for hiding this comment

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

@eAdnan007,

This works well but I am wondering if instead/additionally to add the filter we could compare $_SERVER['HTTP_HOST'] (which includes the port) with a build parsed host that contains the host and the port (if not empty) as wp_parse_url gives us the host and port too. So a user having a different port does not have to have the filter in a MU plugin.

What do you think? Also Mark?
Also if we decide to keep the filter, we might want to document it in the developer docs as extra setup step for user using WordPress on a different port?

@thingalon
Copy link
Member

@eAdnan007,

This works well but I am wondering if instead/additionally to add the filter we could compare $_SERVER['HTTP_HOST'] (which includes the port) with a build parsed host that contains the host and the port (if not empty) as wp_parse_url gives us the host and port too. So a user having a different port does not have to have the filter in a MU plugin.

What do you think? Also Mark?
Also if we decide to keep the filter, we might want to document it in the developer docs as extra setup step for user using WordPress on a different port?

Hmm. I was under the (incorrect) impression that the webserver was running inside docker at port 80, and that docker's external port was just mapping. But... you're right. $_SERVER['HTTP_HOST'] would do the job here. I can't believe I didn't check that.

@eAdnan007 - can you verify that using $_SERVER['HTTP_HOST'] in the check fixes it for you?

haqadn added 3 commits August 26, 2021 14:44
While generating critical CSS, the CSS files need to be proxied through a the plugin to avoid CORS restrictions. Previously the proxy was skipped if the server hostname matched the request. Or if there was no hostname provided in the url. Now, it has a filter hook to allow more control.
The host filter was implemented to overcome the git development issue. But the real problem was that, the hostname was not considering port while comparing.
@haqadn haqadn force-pushed the update/critical-css-proxy-skip-filter branch from 1390075 to 3221a31 Compare August 26, 2021 08:44
@haqadn
Copy link
Contributor Author

haqadn commented Aug 26, 2021

@eAdnan007,

This works well but I am wondering if instead/additionally to add the filter we could compare $_SERVER['HTTP_HOST'] (which includes the port) with a build parsed host that contains the host and the port (if not empty) as wp_parse_url gives us the host and port too. So a user having a different port does not have to have the filter in a MU plugin.

What do you think? Also Mark?
Also if we decide to keep the filter, we might want to document it in the developer docs as extra setup step for user using WordPress on a different port?

Thank you @davidlonjon for keeping the focus on the right thing. Even though the proxy was failing due to docker not being able to external port internally, the actual issue was that the hostname comparison was not fully correct.

I suppose we don't need the filter anymore since I cannot really think of many use cases someone would need to allow cross-origin requests to skip proxy.

@haqadn haqadn requested a review from davidlonjon August 26, 2021 08:48
davidlonjon
davidlonjon previously approved these changes Aug 26, 2021
Copy link
Contributor

@davidlonjon davidlonjon left a comment

Choose a reason for hiding this comment

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

Nice, this looks great to me now 👍

@haqadn haqadn changed the title Boost: Add filter to determine which hosts to skip from critical CSS proxy Boost: Fix skip proxy origins to load css during critical CSS generation Aug 27, 2021
davidlonjon
davidlonjon previously approved these changes Aug 27, 2021
Copy link
Contributor

@davidlonjon davidlonjon left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Member

@thingalon thingalon left a comment

Choose a reason for hiding this comment

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

This does not proxy files on external URLs with no port specified.

e.g.:

add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_style( 'my-plugin-custom-styles', 'https://wordpress.com/calypso/evergreen/10175.fbadd59c9ac7774351b4.min.css' );
} );

--> no proxy. I suspect the issue is the if statement highlighted inline:

// If no domain specified, or domain matches current, no need to proxy.
// Build the resource origin host the requested asset belongs to.
$resource_origin = '';
if ( isset( $parsed['port'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

I suspect this if statement shouldn't be here. Shouldn't $resource_origin always start with $parsed['host']? Otherwise, $resource_origin will be empty for any URL without a port specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a silly mistake 🤦🏽‍♂️

Copy link
Member

@thingalon thingalon left a comment

Choose a reason for hiding this comment

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

👍 Nice!

@thingalon thingalon enabled auto-merge (squash) August 30, 2021 06:14
@thingalon thingalon merged commit 08818f5 into master Aug 30, 2021
@thingalon thingalon deleted the update/critical-css-proxy-skip-filter branch August 30, 2021 06:26
@github-actions github-actions bot added this to the boost/1.3.0 milestone Aug 30, 2021
davidlonjon added a commit that referenced this pull request Aug 31, 2021
* master: (34 commits)
  Fix issue templates (#20880)
  Social Icon Widget: Fix Icons Not Saving (#20862)
  Dashboard: Add a specific message for a site that is too new to have a rewind status (#20863)
  Add additional properties to WC Analytics events (#20812)
  Connection: add spinner for the "Connect" button (#20872)
  Update storybook monorepo to v6.3.7 (#20877)
  Lightweight PHPCS (#20876)
  Recommendations: Add Jetpack Product Suggestions step (#20814)
  Update babel monorepo (#20875)
  Update wordpress monorepo (#20873)
  cli: Check subprocess exit statuses in docker commands (#20861)
  Facebook Widget: Fix URL error when adding widget (#20864)
  Stats: make nudges collapsible (#20765)
  Remove temporary Newspack block override css (#20868)
  Allow ZIP uploads via Calypso (#20860)
  Sync Package Release v1.26.0 (#20870)
  Connection: remove in-place in secondary flows (#20739)
  BUILD_DIR -> BUILD_BASE in initial checks (#20857)
  E2E tests: Fix missing action for atomic workflow (#20866)
  Boost: Fix skip proxy origins to load css during critical CSS generation (#20793)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Boost Feature] Critical CSS Issues involving the Critical CSS feature in Boost [Plugin] Boost A feature to speed up the site and improve performance.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants