performance icon indicating copy to clipboard operation
performance copied to clipboard

Server-Timing: Reuse the WordPress 6.9 template enhancement output buffer

Open MarcinDudekDev opened this issue 5 months ago • 2 comments

Summary

Reuses the WordPress 6.9 template enhancement output buffer (Core-43258, see r60936) for the Server-Timing header, instead of Performance Lab starting its own output buffer.

  • When output buffering is enabled, the header is sent at the wp_finalized_template_enhancement_output_buffer action — after the template has been captured and enhanced. Registering this action also opts in to starting the core output buffer (wp_should_output_buffer_template_for_enhancement()), and core runs the wp_template_enhancement_output_buffer filter (used by Optimization Detective) before this action, so those metrics are captured before the header is sent.
  • When output buffering is disabled, the header is sent at the wp_before_include_template action, right before the template is included.

This removes the custom output buffer (start_output_buffer()), the on_template_include() filter callback, and the "register earliest so the callback flushes last" ordering hack. Net result is less code and no competing/nested output buffer when other consumers (e.g. Optimization Detective) are active.

Fixes #2225.

Dependency

⚠️ This requires the minimum WordPress version to be 6.9 (#2354). The new hooks/functions only exist in 6.9+, and this PR intentionally omits function_exists() guards. Please land after/with the 6.9 minimum-version bump.

Known limitation

In the non-buffered path, wp_before_include_template only fires when the resolved template is a readable file (unlike the old template_include filter, which fired unconditionally). On routes where no readable template is included, the Server-Timing header is not sent. This is an accepted trade-off of reusing the core buffer.

Test plan

  • Updated test-load.php to assert the new hook registrations.
  • --group server-timing: 49 tests, 100 assertions — green on WP 7.0 (which includes the 6.9 API).
  • Full performance-lab testsuite: green.
  • phpcs + phpstan pass.

Note: a true integration test asserting the emitted Server-Timing header is not feasible in the PHPUnit harness because send_header()'s headers_sent() guard trips under CLI; coverage stays at hook-registration level, consistent with the existing tests.

AI assistance

AI assistance: Yes Tool(s): Claude Code (Anthropic) Model(s): Claude Opus 4.8 Used for: Implementation, test updates, and code review (cross-checked with Grok); I have reviewed and take responsibility for the output.

MarcinDudekDev avatar Jun 04 '26 13:06 MarcinDudekDev

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: MarcinDudekDev <[email protected]>
Co-authored-by: westonruter <[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 Jun 04 '26 13:06 github-actions[bot]

Codecov Report

:x: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review. :white_check_mark: Project coverage is 69.38%. Comparing base (347597f) to head (977622e). :warning: Report is 96 commits behind head on trunk.

Files with missing lines Patch % Lines
...udes/server-timing/class-perflab-server-timing.php 50.00% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2515      +/-   ##
==========================================
+ Coverage   69.33%   69.38%   +0.04%     
==========================================
  Files          90       90              
  Lines        7749     7714      -35     
==========================================
- Hits         5373     5352      -21     
+ Misses       2376     2362      -14     
Flag Coverage Δ
multisite 69.38% <50.00%> (+0.04%) :arrow_up:
single 35.80% <0.00%> (+0.07%) :arrow_up:

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

:umbrella: View full report in Codecov by Harness.
: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 Jun 04 '26 13:06 codecov[bot]