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

Prevent optimizing post previews by default #1848

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ function od_can_optimize_response(): bool {
// > Access to script at '.../detect.js?ver=0.4.1' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
// So it's better to just avoid attempting to optimize Post Embed responses (which don't need optimization anyway).
is_embed() ||
// Do not gather url metric for post that is not published yet, see - https://github.com/WordPress/performance/issues/1841.
is_preview() ||
Copy link
Member

Choose a reason for hiding this comment

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

I tried commenting out this line to see if the new test would start to fail as expected, but it still passed. This is because the test's set_up logic wasn't setting the current user to be able to view the post preview in the first place. When this happens, the $wp_query->posts loop is then empty. This results in od_get_cache_purge_post_id() returning null which as can be seen below also results in this od_can_optimize_response() function returning false. So I update the test in e961c37 to explicitly set the current user so that is_preview() will return true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @westonruter, for the fix and insights on this.

// Since injection of inline-editing controls interfere with breadcrumbs, while also just not necessary in this context.
is_customize_preview() ||
// Since the images detected in the response body of a POST request cannot, by definition, be cached.
Expand Down
12 changes: 12 additions & 0 deletions plugins/optimization-detective/tests/test-optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@
},
'expected' => false,
),
'singular_as_post_preview' => array(
'set_up' => static function (): string {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello',
'post_status' => 'draft',
)
);
return get_preview_post_link( $post_id );

Check failure on line 302 in plugins/optimization-detective/tests/test-optimization.php

View workflow job for this annotation

GitHub Actions / PHP

Anonymous function should return string but returns string|null.
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved
},
'expected' => false,
),
'home_post_request_as_anonymous' => array(
'set_up' => static function (): string {
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand Down
Loading