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

Min/max aspect ratios should be be rounded to a fixed nearest decimal #1765

Open
westonruter opened this issue Dec 20, 2024 · 1 comment
Open
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken

Comments

@westonruter
Copy link
Member

Looking at a site in the wild I see the following in the page source:

"minViewportAspectRatio":0.40000000000000002220446049250313080847263336181640625,"maxViewportAspectRatio":2.5

This is a laughable minViewportAspectRatio. Nevertheless, the values 0.4 and 2.5 are what show up in the source:

return (float) apply_filters( 'od_minimum_viewport_aspect_ratio', 0.4 );

return (float) apply_filters( 'od_maximum_viewport_aspect_ratio', 2.5 );

Aren't floating point numbers fun.

I'm not sure how having such a long decimal would be prevented, as the return value of the functions there is sent straight to be exported as JSON here:

$detect_args = array(
'minViewportAspectRatio' => od_get_minimum_viewport_aspect_ratio(),
'maxViewportAspectRatio' => od_get_maximum_viewport_aspect_ratio(),

wp_json_encode( $detect_args )

Not a big issue.

@westonruter westonruter added [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken labels Dec 20, 2024
@github-project-automation github-project-automation bot moved this to Not Started/Backlog 📆 in WP Performance 2024 Dec 20, 2024
@westonruter westonruter changed the title Min/max aspect ratios should be be rounded to the nearest 10s place decimal Min/max aspect ratios should be be rounded to a fixed nearest decimal Dec 20, 2024
@ShyamGadde
Copy link
Contributor

Just thinking out loud here...

When exploring potential solutions, one idea that came to mind was to convert the float values to strings before JSON encoding. Then on the client side, we could convert them back to numbers when needed. This approach seems straightforward, though it introduces an extra step in handling these values.

I also came across another potential solution involving PHP's serialize_precision setting:

$original_precision = ini_get( 'serialize_precision' );
ini_set( 'serialize_precision', -1 );
// ... wp_json_encode( $detect_args ) ...
ini_set( 'serialize_precision', $original_precision );

This seems to address the issue as well, but I'm not entirely sure about the potential pitfalls of changing configuration values at runtime. It doesn’t feel like the cleanest approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken
Projects
Status: Not Started/Backlog 📆
Development

No branches or pull requests

2 participants