Skip to content

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

Open
MarcinDudekDev wants to merge 1 commit into
WordPress:trunkfrom
MarcinDudekDev:feature/2225-server-timing-template-enhancement-buffer
Open

Server-Timing: Reuse the WordPress 6.9 template enhancement output buffer#2515
MarcinDudekDev wants to merge 1 commit into
WordPress:trunkfrom
MarcinDudekDev:feature/2225-server-timing-template-enhancement-buffer

Conversation

@MarcinDudekDev

Copy link
Copy Markdown

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.

…buffer for Server-Timing.

Instead of starting its own output buffer at template_redirect (and sending the
header via the template_include filter when buffering is disabled), the
Server-Timing API now relies on the template enhancement output buffer
introduced in WordPress 6.9 (Core-43258, r60936):

- When output buffering is enabled, the header is sent at the
  wp_finalized_template_enhancement_output_buffer action, which also opts in to
  starting the core output buffer.
- When output buffering is disabled, the header is sent at the
  wp_before_include_template action.

This removes the custom output buffer and its "register earliest to flush last"
ordering hack. Requires the minimum WordPress version to be 6.9.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

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 <myththrazz@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

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

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 69.38%. Comparing base (347597f) to head (977622e).
⚠️ 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 ⚠️
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%) ⬆️
single 35.80% <0.00%> (+0.07%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leverage template enhancement output buffer for Server Timing in WP 6.9

1 participant