Skip to content

Fixed CORS issues and update performance count fetching logic - #7943

Open
FaizanCyber007 wants to merge 6 commits into
layer5io:masterfrom
FaizanCyber007:fix/performance-counter-cors
Open

Fixed CORS issues and update performance count fetching logic#7943
FaizanCyber007 wants to merge 6 commits into
layer5io:masterfrom
FaizanCyber007:fix/performance-counter-cors

Conversation

@FaizanCyber007

@FaizanCyber007 FaizanCyber007 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Tile: Fix performance tests counter showing 0

Description

This PR fixes #7860

The "Performance Tests run" counter on the Meshery pages (/cloud-native-management/meshery/, /cloud-native-management/meshery/operating-cloud-native-infra, and /projects/nighthawk) was permanently stuck at 0 instead of showing the live count (~160k+) from https://cloud.layer5.io/api/performance/results/total.

Root causes:

  1. API field name mismatch — the API's JSON response uses totalRuns (camelCase), but all four components read result.total_runs (snake_case), which is always undefined.
  2. Counter cross-contamination — in Features-Col/index.js, the "cloud native integrations" counter and the "performance tests run" counter shared a single fallback condition (feature.count.value !== 0 ? feature.count.value : performanceCount). Whenever the integrations count is legitimately 0 (e.g. in a local lite dev build), it incorrectly displayed the performance count instead of its own value. Fixed by matching on the feature's identity (feature.count.description === "performance tests run") instead of a zero-sentinel.
  3. Silent fetch failuresCounters/index.js, data-card.js, and Nighthawk/index.js had no .catch() on their fetch chains, so any fetch failure (including CORS, see below) left the counter at 0 with zero console output.

Files changed:

  • src/sections/Counters/index.js
  • src/sections/Meshery/Features-Col/index.js
  • src/sections/Meshery/How-meshery-works/specs/data-card.js
  • src/sections/Projects/Nighthawk/index.js
  • gatsby-config.js

Notes for Reviewers

While verifying this locally, the field-name fix alone wasn't enough to actually see working — cloud.layer5.io's CORS policy only allows https://layer5.io / https://www.layer5.io as origins, so a browser fetch from localhost (any contributor, any machine) is silently blocked. This is unrelated to the bug above and doesn't affect production (already correctly allowed), but it meant no one could visually confirm this fix locally.

To fix that, gatsby-config.js adds a dev-only proxy (developMiddleware, gated behind NODE_ENV === "development") that routes /api/* requests through the dev server's own origin, avoiding the cross-origin request entirely. It's implemented with Node's built-in https module rather than Gatsby's own built-in proxy config, because that built-in feature has an existing bug in this Gatsby/Node version combination — its req.pipe(got.stream(...)) wiring breaks TLS certificate verification for GET requests (reproduced deterministically while debugging this).

This proxy has zero effect on gatsby build/production:

  • Ran a real NODE_ENV=production build and confirmed the built JS bundles contain the direct cloud.layer5.io URL with no trace of the dev-only proxy anywhere in public/.
  • Served that production build (gatsby serve) and confirmed all affected pages return 200 with no runtime errors.

Verified the fix visually against the dev server with a real Chrome instance — the performance counter genuinely animates from 0 up to the live API value, and the three counters on the Meshery page ("cloud native integrations", "standardized components", "performance tests run") now render independently instead of collapsing onto the same number. Recording attached.

Note: the "cloud native integrations" counter itself needed no change — it already renders the correct live count (~390) in production (verified directly against the production site's data). It shows 0 in local lite dev builds only, because develop:lite intentionally excludes the integrations content collection to keep local builds fast — pre-existing, unrelated to this fix.


Demo Video:

Recording.2026-08-10.021639.mp4

Signed commits

  • [ ✔️ ] Yes, I signed my commits.

Summary by CodeRabbit

  • New Features

    • Added development-only proxy support for cloud API requests.
    • Performance counters now display current run totals from the cloud service.
  • Bug Fixes

    • Corrected performance metrics to use updated response data.
    • Improved handling and logging when performance data requests fail.
  • Style

    • Reformatted service icons, data cards, counters, and the Nighthawk page without changing displayed content.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: be24a062-bdbf-4ffa-bc61-45ab174f63f4

📥 Commits

Reviewing files that changed from the base of the PR and between 02ac6b0 and 3818b99.

📒 Files selected for processing (1)
  • src/sections/Meshery/How-meshery-works/specs/data-card.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/sections/Meshery/How-meshery-works/specs/data-card.js

📝 Walkthrough

Walkthrough

Development builds now proxy /api/* requests to cloud.layer5.io. Performance counters select environment-specific endpoints, validate totalRuns, update valid counts, and log failures. JSX and styling blocks were reformatted without changing rendered behavior.

Changes

Performance API integration

Layer / File(s) Summary
Development proxy and endpoint selection
gatsby-config.js, src/sections/Counters/index.js, src/sections/Meshery/Features-Col/index.js
Development mode proxies /api/* requests to cloud.layer5.io. Performance fetches select the proxy in development and the cloud endpoint otherwise.
Performance response consumption
src/sections/Counters/index.js, src/sections/Meshery/Features-Col/index.js, src/sections/Meshery/How-meshery-works/specs/data-card.js, src/sections/Projects/Nighthawk/index.js
Performance fetches validate HTTP responses and totalRuns. Counters update from valid values and log failures. Performance features use the fetched count, while other features use configured values.
Feature and page presentation updates
src/sections/Meshery/Features-Col/index.js, src/sections/Meshery/How-meshery-works/specs/data-card.js, src/sections/Projects/Nighthawk/index.js
JSX, SVG, styled-component, and content formatting changed without changing rendered behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PerformanceCounter
  participant GatsbyMiddleware
  participant CloudAPI
  PerformanceCounter->>GatsbyMiddleware: Request performance count through /api/*
  GatsbyMiddleware->>CloudAPI: Forward HTTPS request
  CloudAPI-->>GatsbyMiddleware: Return response status, headers, and body
  GatsbyMiddleware-->>PerformanceCounter: Relay response or return HTTP 502 JSON
Loading

Possibly related issues

Possibly related PRs

  • layer5io/layer5#7870: Updates the same performance-counter components to read totalRuns and handle fetch errors.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies both main changes: the development proxy for CORS issues and the performance count fetching updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed: dependency version conflict. Check your lock file or package.json.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
src/sections/Meshery/Features-Col/index.js (1)

85-90: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Stabilize performanceCountEndpoint for the effect.

performanceCountEndpoint is declared inside Features, but the effect has an empty dependency array at Line 110. If react-hooks/exhaustive-deps is enabled, ESLint reports the endpoint reference as missing. Move the endpoint constant to module scope or include it in the dependency list.

As per coding guidelines, run npm run lint and ensure ESLint and Prettier checks pass.

Also applies to: 102-110

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/sections/Meshery/Features-Col/index.js` around lines 85 - 90, Move
performanceCountEndpoint outside the Features component to module scope, keeping
its development and production values unchanged, so the effect’s empty
dependency array no longer captures a component-local value. Run npm run lint
and resolve any ESLint or Prettier issues.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@gatsby-config.js`:
- Around line 57-64: Update the proxy middleware around app.use("/api/*") to
either narrow the route to the intended GET endpoint or fully proxy non-GET
requests by forwarding the request body and required headers, including
Content-Type, cookies, and authorization data, and writing the body before
proxyReq.end().
- Around line 59-75: Update the proxy request created in the https.request block
to configure proxyReq.setTimeout, destroy the stalled request when the timeout
fires, and return a 502 response only when res.headersSent is false. Preserve
the existing proxyReq error handling while applying the same headers-sent guard
before writing the failure response.

In `@src/sections/Counters/index.js`:
- Around line 19-24: Validate the performance API response before updating
state: in src/sections/Counters/index.js lines 19-24,
src/sections/Meshery/How-meshery-works/specs/data-card.js lines 50-55, and
src/sections/Projects/Nighthawk/index.js lines 27-32, require response.ok and a
finite result.totalRuns before calling setPerformanceCount; ensure invalid
status, malformed JSON, or missing values flow to the existing error handling.

In `@src/sections/Meshery/Features-Col/index.js`:
- Around line 102-108: Remove the performance-count assignment to 250000 in the
response.ok failure path, allowing the existing default value of 0 to remain
when the request fails. Preserve the error throw and the catch handling in the
performance-count fetch flow.

---

Nitpick comments:
In `@src/sections/Meshery/Features-Col/index.js`:
- Around line 85-90: Move performanceCountEndpoint outside the Features
component to module scope, keeping its development and production values
unchanged, so the effect’s empty dependency array no longer captures a
component-local value. Run npm run lint and resolve any ESLint or Prettier
issues.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a9c86257-ae5a-41a6-a348-360c7b456146

📥 Commits

Reviewing files that changed from the base of the PR and between 264856f and 5e91913.

📒 Files selected for processing (5)
  • gatsby-config.js
  • src/sections/Counters/index.js
  • src/sections/Meshery/Features-Col/index.js
  • src/sections/Meshery/How-meshery-works/specs/data-card.js
  • src/sections/Projects/Nighthawk/index.js

Comment thread gatsby-config.js Outdated
Comment thread gatsby-config.js
Comment thread src/sections/Counters/index.js
Comment thread src/sections/Meshery/Features-Col/index.js
@FaizanCyber007

Copy link
Copy Markdown
Contributor Author

@KhushamBansal, please review it and help me to identify some more issues need to be fixed

@KhushamBansal KhushamBansal 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.

@FaizanCyber007 Could you please address feedback by coderabbit?

FaizanCyber007 and others added 3 commits August 10, 2026 14:36
Signed-off-by: Faizan Javed <faizanjaved246@gmail.com>
The proxy only forwards method and accept header, not body/Content-Type/
auth/cookies, so non-GET requests were silently broken. All current
frontend usage of the relative /api/* path is GET-only, so scope the
route accordingly instead of building out unused forwarding.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Faizan Javed <faizanjaved246@gmail.com>
Add timeout + destroy on the dev proxy request and guard the error
handler with res.headersSent so a mid-stream failure doesn't attempt
a second response. Validate the performance API response (status and
a finite totalRuns) before updating state in Counters, DataCard, and
Nighthawk. Remove the hardcoded 250000 fallback in Features-Col so the
default count of 0 is preserved on fetch failure.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Faizan Javed <faizanjaved246@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/sections/Meshery/How-meshery-works/specs/data-card.js`:
- Around line 58-61: Update the totalRuns validation before setPerformanceCount
in the performance result handling to require a finite, non-negative integer,
rejecting negative and fractional values while preserving the existing
invalid-count error behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 66015fbe-a470-4716-8935-77d1cc2afe92

📥 Commits

Reviewing files that changed from the base of the PR and between 533a1f9 and 02ac6b0.

📒 Files selected for processing (5)
  • gatsby-config.js
  • src/sections/Counters/index.js
  • src/sections/Meshery/Features-Col/index.js
  • src/sections/Meshery/How-meshery-works/specs/data-card.js
  • src/sections/Projects/Nighthawk/index.js
💤 Files with no reviewable changes (1)
  • src/sections/Meshery/Features-Col/index.js
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/sections/Projects/Nighthawk/index.js
  • src/sections/Counters/index.js
  • gatsby-config.js

Comment thread src/sections/Meshery/How-meshery-works/specs/data-card.js Outdated
@FaizanCyber007
FaizanCyber007 force-pushed the fix/performance-counter-cors branch from 02ac6b0 to 59173db Compare August 10, 2026 14:37
@github-actions

github-actions Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Preview deployment for PR #7943 removed.

This PR preview was automatically pruned because we keep only the 3 most recently updated previews on GitHub Pages to stay within deployment size limits.

If needed, push a new commit to this PR to generate a fresh preview.

Number.isFinite allowed negative or non-integer values through to
setPerformanceCount; require a finite, non-negative integer instead.

Signed-off-by: Faizan Javed <faizanjaved246@gmail.com>
@FaizanCyber007

Copy link
Copy Markdown
Contributor Author

@KhushamBansal , I have implemented the changes suggested by Coderabbit. You can check them now and let me know

// so local `gatsby develop` sessions use the dev-only proxy from gatsby-config.js instead.
export const URL =
process.env.NODE_ENV === "development"
? "/api/performance/results/total"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Heads-up: this endpoint is never locally served by the layer5.io website.

@leecalcote

Copy link
Copy Markdown
Member

A duplicate of #7867?

@FaizanCyber007

Copy link
Copy Markdown
Contributor Author

@leecalcote Confirmed — #7867, #7870, and this PR all fix the same root cause for #7860. Good catch.
What sets this one apart: #7867 and #7870 both admit they can't be verified locally — CORS blocks the API from localhost, so both fixes are unverified guesses. This PR adds a dev proxy that fixes that, so the fix is provably working — see the attached demo. It's also scoped cleanly: #7867 has an unresolved lint-noise complaint, #7870 has an open CodeRabbit warning for unrelated formatting changes.
Gap to close: the other two cover 4 components (Counters, Features-Col, data-card, Nighthawk); this PR currently covers 2. I'll extend it to all 4 with one shared parser (using ??, not || — #7870's fallback silently drops a valid 0).
Once that's done, this is the only verifiable, complete fix for #7860. Happy to consolidate here, or elsewhere if you'd rather — your call.

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.

Inaccurate: "0" performance tests results being reported

4 participants