Skip to content

Premium Analytics: own the status-preserving parse at the data layer fetch boundary - #50885

Merged
chihsuan merged 3 commits into
trunkfrom
wooa7s-1802-fetch-boundary-parse
Jul 31, 2026
Merged

chihsuan merged 3 commits into
trunkfrom
wooa7s-1802-fetch-boundary-parse

Conversation

@chihsuan

@chihsuan chihsuan commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

Fixes WOOA7S-1802

Follow-up to #50727, which restored the HTTP status on failed Stats requests with a global apiFetch middleware. This moves that logic to the data layer's own fetch boundary.

Proposed changes

  • Parse Stats responses in stats-proxy-fetch.ts: fetchStatsProxy() requests with parse: false, normalizes the response, and attaches response.status on failure.
  • Delete error-status-middleware.ts, its registration in query-client-provider.tsx, and the apiFetch.use capability guard — packages/data no longer touches apiFetch's global middleware chain.
  • Share the Response shape check report-export-fetch.ts already had, as api/is-response.ts.
  • Port the middleware tests onto fetchStatsProxy / fetchReport, so none of them depend on a global registration.

In a shared chain the fix only worked if the app's boot path ran, and on WP 7.0.x it doesn't (#50309) — so #50727 registered at module load, which then needed a typeof apiFetch.use !== 'function' guard that exists only because unit suites mock the module. That guard goes away here, as do the parse: false and per_page=-1 pass-throughs that only existed to coexist with middlewares we don't control. The already-parsed branch stays: Storybook's report mocks short-circuit with plain data regardless of parse.

One thing to flag: the moved code shape-checks Response instead of using instanceof. jsdom has no Response constructor, so on the data layer's normal path instanceof throws in every suite — and guarding it with typeof Response !== 'undefined' would just be the test-shaped conditional this PR removes elsewhere.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

A refactor with no user-facing change, so this is all about confirming nothing regressed.

  • jetpack test js packages/premium-analytics — 194 suites / 1340 tests pass. The ported status tests are in packages/data/src/api/__tests__/stats-proxy-fetch-response.test.ts: 401/502/503/504, an HTML 502 that must keep its status despite unparseable JSON, 204, a body carrying its own status, and a non-Response rejection rethrown untouched.
  • pnpm run storybook:dev in projects/js-packages/storybook, then Packages/Premium Analytics/Widgets/SearchTerms — confirms the mocks still intercept and error mapping still reads the status. Default renders rows; Error (permission 403) shows "You don't have access to this data." with no Retry; Error Retryable (no_connection 403) shows Retry.
  • On a real site, disconnect the site (or block /jetpack-premium-analytics/v1/proxy/ in devtools) and confirm widgets land on the retryable error state instead of retrying a 403 three times.

@chihsuan chihsuan self-assigned this Jul 29, 2026
@github-actions

github-actions Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the wooa7s-1802-fetch-boundary-parse branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack wooa7s-1802-fetch-boundary-parse
bin/jetpack-downloader test jetpack-mu-wpcom-plugin wooa7s-1802-fetch-boundary-parse

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions

github-actions Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jul 29, 2026
@jp-launch-control

jp-launch-control Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 4 files.

File Coverage Δ% Δ Uncovered
projects/packages/premium-analytics/packages/data/src/api/stats-proxy-fetch.ts 52/52 (100.00%) 0.00% 0 💚
projects/packages/premium-analytics/packages/data/src/providers/query-client-provider.tsx 12/19 (63.16%) -1.84% 0 💚
projects/packages/premium-analytics/packages/data/src/api/report-export-fetch/report-export-fetch.ts 35/40 (87.50%) 2.13% -1 💚
projects/packages/premium-analytics/packages/data/src/queries/stats-app-notices-query.ts 5/6 (83.33%) 16.67% -1 💚

1 file is newly checked for coverage.

File Coverage
projects/packages/premium-analytics/packages/data/src/api/is-response.ts 1/1 (100.00%) 💚

Full summary · PHP report · JS report

@chihsuan
chihsuan marked this pull request as ready for review July 29, 2026 04:08
@chihsuan
chihsuan requested a review from a team July 29, 2026 04:08
@chihsuan
chihsuan requested a review from a team as a code owner July 29, 2026 04:08
@chihsuan chihsuan added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Status] In Progress labels Jul 29, 2026

@kangzj kangzj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactoring.

@chihsuan
chihsuan merged commit 46362b3 into trunk Jul 31, 2026
82 checks passed
@chihsuan
chihsuan deleted the wooa7s-1802-fetch-boundary-parse branch July 31, 2026 05:59
@github-actions github-actions Bot removed [Status] In Progress [Status] Needs Review This PR is ready for review. labels Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants