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

Module Proposal: Alternative LCP Optimization (via Output Buffering) #805

Closed
Tracked by #869
westonruter opened this issue Aug 16, 2023 · 1 comment · Fixed by #875
Closed
Tracked by #869

Module Proposal: Alternative LCP Optimization (via Output Buffering) #805

westonruter opened this issue Aug 16, 2023 · 1 comment · Fixed by #875
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin

Comments

@westonruter
Copy link
Member

westonruter commented Aug 16, 2023

Overview

  • Proposed module name: Alternative LCP Optimization
  • Proposed module slug: alternative-lcp-optimization
  • Proposed module owner GitHub username(s): westonruter

About the module

Purpose

Disable core's insertion of fetchpriority="high" and loading="lazy" attributes during template rendering to instead insert them on the fully-rendered template captured via output buffering. The module would explore whether this is feasible and whether it produces better results.

Scope

  • Add output buffering of the template in a similar way as Server-Timing is doing already. A separate output buffer can be used specific for this module without needing to reuse the one from Server-Timing.
  • Introduce a filter in the output buffer callback. This would be implementing Core-43258 which could be reused by plugins seeking to do optimizations or by caching plugins to store the rendered page.
  • Use WP_HTML_Tag_Processor to iterate over the HTML document and inject the fetchpriority="high" and loading="lazy" attributes. This may involve the use of bookmarks to gather up a set of candidates, also informed by existing heuristics during template generation.

Rationale

WordPress currently optimized LCP elements by injecting fetchpriority="high" and omitting loading="lazy" during template rendering. This has been the only option available for core because core lacks any standardized mechanism for output buffering and filtering the output of the page, even though a multitude of plugins do so (including the Performance Lab plugin to do Server-Timing).

As noted in #793, there are currently some cases where the optimization attributes are not applied correctly, due to the complex and varied mechanisms that WordPress core and plugins use to render templates. Some themes and plugins may even hard-code img tags in which case it is currently impossible to apply the correct attributes.

If the rendered template were output buffered, then the optimization attributes could be applied once on the entire document before the complete HTML page is returned to the user.

Note that block themes are already essentially output buffered, although without using ob_start().

@felixarntz
Copy link
Member

PR #875 merged into feature branch.

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
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

4 participants