fix: cache OpenAPI spec in sessionStorage to avoid fetch on every mount - #673
Open
Heazzy500 wants to merge 1 commit into
Open
fix: cache OpenAPI spec in sessionStorage to avoid fetch on every mount#673Heazzy500 wants to merge 1 commit into
Heazzy500 wants to merge 1 commit into
Conversation
…nt (Savitura#580) The OpenAPI spec was fetched from the network every time the Developer page was mounted, causing unnecessary network requests and slower page load. Cache the parsed endpoints in sessionStorage so subsequent mounts within the same session skip the network roundtrip. The cache key includes the API base URL for correct isolation across deployments. Corrupted cache entries are silently discarded and re-fetched.
|
@Heazzy500 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR fixes a low-severity performance bug in the Developer page where the OpenAPI specification (
/api/v1/docs/openapi.json) was fetched from the network on every single mount ofsrc/pages/Developer.jsx. Since the spec is static for the lifetime of a deployment and is unlikely to change within a browser session, this caused:/developertriggered afetchto the APIThe fix caches the parsed endpoint list in
sessionStoragewith a deployment-scoped cache key. Subsequent mounts within the same session read from cache instantly, eliminating the network roundtrip. The cache is automatically invalidated when the user closes the tab (session storage), which is the correct TTL — the spec only changes on deployment restart.Related Issue
Closes #580
Changes
[FIX]
frontend/src/pages/Developer.jsx—loadOpenApifunctionProblem
The
loadOpenApifunction (lines 61-113) unconditionally fetches the OpenAPI spec on every mount:This means:
/dashboard→ navigate back to/developer→ re-fetches spec/campaigns→ navigate back to/developer→ re-fetches specFix
A
sessionStoragecache check is inserted at the top ofloadOpenApi, before the network fetch:After a successful network fetch and parse, the result is written to cache:
Design decisions
sessionStorageoverlocalStoragesessionStorageavoids stale-cache bugs if the deployment updates mid-session (rare but possible with hot-reload dev setups).sessionStorageover in-memory/statesessionStoragesurvives reloads while still being session-scoped.sessionStorageover TTL-based cachingsessionStorage's lifetime is the natural TTL here — the spec is deployment-static and the session ends on tab close. No timer management needed.V1_API_BASE(cp_openapi_spec_${V1_API_BASE}), so different deployments (staging vs production) get separate cache entries.JSON.parsefails on a cached entry (e.g., truncated write), the corrupt entry is removed and the spec is re-fetched. No user-facing error — the page still works. Thetry/catchonsessionStorage.setItemhandles the edge case where storage is full or disabled (private browsing in some browsers).Array.isArray(cached) && cached.length > 0) before being used, preventing empty or malformed cache entries from rendering a broken API explorer.explorerEndpointfromlocalStorageexactly as the original code did after a network fetch, so the user's last-used endpoint is preserved across cached and non-cached code paths.No changes to other storage usage
The component already uses
localStorageforcp_explorer_endpoint(persisting the selected endpoint across sessions) andcp_explorer_params_*(persisting form state with a 400ms debounce). These are intentionally left aslocalStorage— endpoint selection is user preference that should survive sessions, and parameter persistence is a convenience feature. Only the fetched data (the spec) is moved tosessionStorage.Files Changed
frontend/src/pages/Developer.jsxloadOpenApiVerification Results
Full test suite
✅ 153/153 tests pass — no regressions. All 44 test files pass, including existing component tests that exercise the Developer page indirectly.
Manual verification checklist (for reviewer)
/developer— API explorer populates normally/developer— API explorer populates instantly (cache hit)cp_openapi_spec_*key exists with valid JSON/developer— spec is re-fetched (session storage cleared)/developer— cache is used (session storage survives reload)Acceptance Criteria
sessionStorage.setItem(cacheKey, JSON.stringify(endpoints))after successful fetchfetch()when cache hit is validtry/catcharoundJSON.parse; corrupted entry removed before re-fetchtry/catcharoundsessionStorage.setItemV1_API_BASElocalStorage.getItem('cp_explorer_endpoint')check on cache hit pathOut of Scope (intentional)
Cache-Controlheaders. This is a backend concern and not addressed here.sessionStorageis already ephemeral.usePreload.jsor a route loader, but this would be a broader architectural change outside the scope of this bug fix.