Skip to content

Optimization Detective should be disabled by default on post previews #1841

@westonruter

Description

@westonruter

I just realized that the the logic in od_can_optimize_response() is accounting for Customizer preview (is_customize_preview()) but not post previews (is_preview()). It doesn't make sense to collect a URL Metric for a post that isn't fully published yet, so is_preview() should be added as a condition to:

$able = ! (
// Since there is no predictability in whether posts in the loop will have featured images assigned or not. If a
// theme template for search results doesn't even show featured images, then this wouldn't be an issue.
is_search() ||
// Avoid optimizing embed responses because the Post Embed iframes include a sandbox attribute with the value of
// "allow-scripts" but without "allow-same-origin". This can result in an error in the console:
// > 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() ||
// 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.
( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) ||
// Page caching plugins can only reliably be told to invalidate a cached page when a post is available to trigger
// the relevant actions on.
null === od_get_cache_purge_post_id()
);

Metadata

Metadata

Assignees

Labels

Type

No fields configured for Bug.

Projects

Status

Done 😃

Relationships

None yet

Development

No branches or pull requests

Issue actions