-
Notifications
You must be signed in to change notification settings - Fork 801
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
Conversation
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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. Boost plugin:
|
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.
@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. @eAdnan007 - can you verify that using |
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.
1390075
to
3221a31
Compare
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. |
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.
Nice, this looks great to me now 👍
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.
👍
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 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'] ) ) { |
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 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.
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.
That is a silly mistake 🤦🏽♂️
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.
👍 Nice!
* 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) ...
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 throughPORT_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:
PORT_WORDPRESS
in tools/docker/.env. The value should be something other than 80(as that would previously break). Read more.