Skip to content

fix: prevent infinite API loop on invalid environment URLs#7284

Open
talissoncosta wants to merge 2 commits intomainfrom
fix/environment-ready-checker-infinite-loop
Open

fix: prevent infinite API loop on invalid environment URLs#7284
talissoncosta wants to merge 2 commits intomainfrom
fix/environment-ready-checker-infinite-loop

Conversation

@talissoncosta
Copy link
Copy Markdown
Contributor

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Closes #7283

When the URL contained an environment key that didn't belong to the current project (deleted env, typo, env from another project, stale bookmark, or permission revoked mid-session), the app entered an infinite API request loop and showed a sidebar loader that never resolved. Further requests appeared with the literal string undefined in the path.

Root cause

Two bugs compounded:

  1. EnvironmentReadyChecker polled every 1s with no error off-ramp. It only stopped polling on a successful response with is_creating: false — on a 4xx it kept polling forever.
  2. Project scoping wasn't checked. The backend returns a single environment by api_key regardless of project scope, so a valid env key belonging to a different project returns 200 OK and the checker falls through to render the page. Downstream components then fire queries with missing or stale env context — some using env?.api_key in URL templates without a fallback, producing /api/v1/environments/undefined/... URLs.

Sibling sidebar components (EnvironmentAside, EnvironmentNavbar) conflated "env list loading" with "env missing from list" and showed loaders forever.

Fix

  • EnvironmentReadyChecker: source of truth is now the project's env list (useGetEnvironmentsQuery). If the URL env isn't in the list, render an "Environment not found" state. The single-env poll is kept for the is_creating happy path but now skips once polling is complete or the env is known missing.
  • State reset during render (not via useEffect) when the env ID changes, so recovering via URL change is instant and doesn't flash stale children.
  • Extracted the three view states into LoadingState, CreatingState, NotFoundState components; render flow is now guard clauses instead of nested ternaries.
  • Handled the /environment/create sentinel so the new-env form isn't caught.
  • EnvironmentAside: hides the whole sidebar when the env isn't in the project list.
  • EnvironmentNavbar: skip: !environment on the change-request queries (no more undefined URL fetches); returns null when env isn't in the list.
  • Small cleanup: dropped redundant `${projectId}` template coerces in the two files touched (fixes pre-existing string not assignable to number TS errors).

How did you test this code?

Manual verification on local dev server (ENV=local npm run dev):

  • /project/<id>/environment/<invalid-key>/features → shows "Environment not found" in the main area, sidebar hidden, no repeat requests in Network tab.
  • /project/<id>/environment/<valid-key>/features → loads features normally.
  • /project/<id>/environment/create → create form renders (sentinel preserved).
  • From an invalid env → click top tabs / env selector → navigate into a valid env → app recovers without reload.
  • Confirmed no /environments/undefined/... URLs fire in any of the above flows.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
flagsmith-frontend-preview Ready Ready Preview, Comment Apr 20, 2026 2:59pm
flagsmith-frontend-staging Ready Ready Preview, Comment Apr 20, 2026 2:59pm
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Ignored Ignored Preview Apr 20, 2026 2:59pm

Request Review

@github-actions github-actions bot added the front-end Issue related to the React Front End Dashboard label Apr 17, 2026
@talissoncosta talissoncosta changed the title Infinite API request loop when URL points to a non-existent environment fix: prevent infinite API loop on invalid environment URLs Apr 17, 2026
@talissoncosta talissoncosta marked this pull request as ready for review April 20, 2026 13:54
@talissoncosta talissoncosta requested a review from a team as a code owner April 20, 2026 13:54
@talissoncosta talissoncosta requested review from Zaimwa9 and removed request for a team April 20, 2026 13:54
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

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

⚠️ Code review skipped — your organization's overage spend limit has been reached.

Code review is billed via overage credits. To resume reviews, an organization admin can raise the monthly limit at claude.ai/admin-settings/claude-code.

Once credits are available, reopen this pull request to trigger a review.

@github-actions github-actions bot added the fix label Apr 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 20, 2026

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-api-test:pr-7284 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-e2e:pr-7284 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api:pr-7284 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-7284 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-7284 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-frontend:pr-7284 Finished ✅ Results

@talissoncosta talissoncosta marked this pull request as draft April 20, 2026 14:00
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 20, 2026

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  53.4 seconds
commit  1cb2d03
info  🔄 Run: #16191 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  22.3 seconds
commit  1cb2d03
info  🔄 Run: #16191 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  54.5 seconds
commit  1cb2d03
info  🔄 Run: #16191 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  1 minute, 3 seconds
commit  1cb2d03
info  🔄 Run: #16191 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  24.6 seconds
commit  3ffe262
info  🔄 Run: #16195 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  11 passed

Details

stats  11 tests across 8 suites
duration  1 minute, 1 second
commit  3ffe262
info  🔄 Run: #16195 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  44.8 seconds
commit  3ffe262
info  🔄 Run: #16195 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  1 minute, 4 seconds
commit  3ffe262
info  🔄 Run: #16195 (attempt 1)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 20, 2026

Visual Regression

16 screenshots compared. See report for details.
View full report

Drop the separate env-list fetch and verify project scope directly from
the single-env query's `project` field. Also make the polling interval
reactive to `is_creating`, removing the `pollingDone` / `trackedEnvId`
state and the effect that managed it. Treat `isError` as not-found,
covering permission-revoked and deleted-env cases.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copy link
Copy Markdown
Contributor

@Zaimwa9 Zaimwa9 left a comment

Choose a reason for hiding this comment

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

Works as a charm

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

Labels

fix front-end Issue related to the React Front End Dashboard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Infinite API request loop when URL points to a non-existent environment

2 participants