performance icon indicating copy to clipboard operation
performance copied to clipboard

Add gzip compression for URL metrics using Compression Streams API

Open b1ink0 opened this issue 8 months ago • 3 comments

Summary

Fixes #1893

Relevant technical choices

This PR implements the Compression Streams API to efficiently compress URL Metrics JSON before sending it. This significantly reduces the payload size, allowing more data to be sent without exceeding the 64 KiB limit.

Previously some of these changes where reverted due to the bug #1928.

This PR is able to avoid the issue of URL metrics not being sent on pagehide event by compressing the URL metrics before pagehide event and then recompressing it whenever URL metrics are updated by the extensions.

b1ink0 avatar Mar 27 '25 17:03 b1ink0

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 72.51%. Comparing base (583a4e9) to head (6aa261c). Report is 27 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1959      +/-   ##
==========================================
+ Coverage   72.34%   72.51%   +0.17%     
==========================================
  Files          85       85              
  Lines        6979     7023      +44     
==========================================
+ Hits         5049     5093      +44     
  Misses       1930     1930              
Flag Coverage Δ
multisite 72.51% <100.00%> (+0.17%) :arrow_up:
single 40.33% <0.00%> (-0.26%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Mar 27 '25 18:03 codecov[bot]

If we do this, we should add a pagehide event handler before we load any of the extensions, so that our pagehide callback is called first, which then causes extendElementData and extendRootData to no-op (or throw an error) because at this point we can't re-compress the URL Metric since it is too late.

https://github.com/WordPress/performance/issues/1893#issuecomment-2728201130

I would like to understand more about adding a pagehide event handler before loading any extensions. From my interpretation, the goal is to override the extendElementData and extendRootData functions to throw an error if an extension tries to modify URL metrics after the pagehide event.

However, we're currently still supporting the finalize method, even though it's deprecated. Introducing this change might cause issues for extensions that still rely on finalize.

At the moment, I’ve added a flag called enableCompression, which is set to false if a finalize function is detected. This effectively disables compression. I could use this same flag to prevent the function overrides that would otherwise throw errors.

cc: @westonruter

b1ink0 avatar Mar 27 '25 18:03 b1ink0

I would like to understand more about adding a pagehide event handler before loading any extensions. From my interpretation, the goal is to override the extendElementData and extendRootData functions to throw an error if an extension tries to modify URL metrics after the pagehide event.

@b1ink0 I think that comment I made is obsolete. We should prevent compression from happening if there is a finalize function exported from an extension, like you're doing in the PR.

westonruter avatar Mar 27 '25 18:03 westonruter

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: b1ink0 <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: phanduynam <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

github-actions[bot] avatar Mar 31 '25 06:03 github-actions[bot]

This issue has been resolved, please close it admin

phanduynam avatar Apr 03 '25 13:04 phanduynam