Skip to content

P2: News block remote fetching — batch requests, add timeouts, fix cache keys, cap pagination #107

Description

@Herm71

Roadmap items 14, 15, 16, 19 (P2). Grouped: one caching/fetching pass over the remote News path. 14 and 16 are verified.

The News block is the only block that fetches over REST from the news site (News_Request, base URL chosen from wp_get_environment_type()). Its fetch path currently has four compounding problems.

Roadmap item 14 — N+1 blocking HTTP on cold render

One wp_remote_get per featured image plus one per coauthor, on top of the posts request itself — up to ~19 sequential blocking calls to render a 9-post block with a cold cache.

Fix: request _embed on the posts request so media and author data arrive in one response; drop the per-object follow-up calls.

📍 src/Components/News_Block_Controller.php:132-190

Roadmap item 15 — No timeout on any remote request

Every wp_remote_get uses the 5s default. Combined with 14, a slow or unreachable news host can stall a front-end page render for tens of seconds.

Fix: set an explicit short timeout (2–3s), and make a failed fetch degrade to an empty/cached result rather than blocking.

📍 src/Request/News_Request.php:17

Roadmap item 16 — Cache keys embed taxonomy_ids

Media and coauthor transients are keyed with the block's taxonomy_ids, so the same attachment or author is cached separately for every block configuration that references it. Cache hit rate collapses; transient table grows.

Fix: key per-object caches by object ID only. Keep the taxonomy-scoped key for the posts query result, where it belongs.

📍 src/Components/News_Block_Controller.php:125-130

Roadmap item 19 — Unbounded pagination recursion

News_Request::request() recurses over paginated results with no page cap. A large X-WP-TotalPages means unbounded sequential fetches on a single render.

Fix: cap the page count (and/or stop once enough posts for the block have been collected).

📍 src/Request/News_Request.php:30-40

Acceptance

  • Cold render of a 9-post News block makes a small bounded number of HTTP requests (target: 1–2)
  • Explicit timeout on every remote call; unreachable host degrades gracefully instead of hanging the render
  • Per-object transients shared across block configurations
  • Hard cap on pages fetched per request
  • Measured before/after request count and wall time noted on the PR

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Performance & lifecycleperformancePerformance and resource usage

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions