Skip to content

feat(settings): auto-sync RPCs by latency on first load#285

Merged
AugustoL merged 7 commits intoopenscan-explorer:devfrom
AugustoL:feat/rpc-auto-sync
Mar 9, 2026
Merged

feat(settings): auto-sync RPCs by latency on first load#285
AugustoL merged 7 commits intoopenscan-explorer:devfrom
AugustoL:feat/rpc-auto-sync

Conversation

@AugustoL
Copy link
Collaborator

@AugustoL AugustoL commented Mar 9, 2026

Description

Automatically tests and re-orders RPC endpoints by latency and privacy tier on first load, so the best RPCs are always at the front without requiring manual action via the Settings sync button.

Related Issue

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Performance improvement
  • Other (please describe):

Changes Made

  • Extracted getPrivacyTier and sortRpcsByQuality from settings/index.tsx into a new shared utility src/utils/rpcAutoSync.ts
  • Added autoSyncRpcs() function that tests all networks with ≥2 RPC endpoints in parallel and returns them sorted by quality
  • Created src/hooks/useRpcAutoSync.ts hook that triggers auto-sync once on first load (when networksLoading is false and rpcsSynced is falsy)
  • Added rpcsSynced?: boolean to UserSettings type to persist the sync state
  • Added updateSettings({ rpcsSynced: false }) in the "Clear Cache Data" handler so the next load re-syncs
  • Wired useRpcAutoSync() into AppContent in App.tsx

Behavior

  • First load: burst of parallel RPC test requests; OPENSCAN_RPC_URLS_V3 rewritten with sorted order; rpcsSynced: true saved
  • Subsequent loads: sync skipped (flag is set)
  • "Clear Cache Data": resets rpcsSynced: false → next reload re-syncs
  • "Clear Site Data": wipes all localStorage → next reload re-syncs
  • Network with 1 RPC: skipped (nothing to reorder)
  • All RPCs offline: order unchanged; rpcsSynced NOT set (retries on next load)

Checklist

  • I have run npm run format:fix and npm run lint:fix
  • I have run npm run typecheck with no errors
  • I have run tests with npm run test:run
  • I have tested my changes locally
  • I have updated documentation if needed
  • My code follows the project's architecture patterns

Additional Notes

The syncedRef in useRpcAutoSync prevents a double-run in React StrictMode (which mounts effects twice in development).

Automatically tests and re-orders RPC endpoints by quality (privacy
tier + latency) on first load, removing the need for manual sync.
A rpcsSynced flag prevents re-running on every page load and is reset
when the user clears cache data.
@github-actions
Copy link

github-actions bot commented Mar 9, 2026

🚀 Preview: https://pr-285--openscan.netlify.app
📝 Commit: 42685667a478f688a57e8a38fb5c401c5140b52a

@AugustoL AugustoL requested a review from MatiasOS March 9, 2026 15:03
@josealoha666
Copy link
Collaborator

Re-checked the latest version of this PR after the follow-up commits.

The main concerns I had before are now addressed:

  • auto-sync no longer updates in-memory RPC state during the active session, so it avoids mid-page data refetches/churn
  • timeout was reduced from 10s to 5s, which makes the background probe burst less costly
  • sync is now delayed until the route settles, which is a much better fit for initial page UX
  • using location.key for the settle debounce is a nice refinement over pathname

At this point I think the tradeoff looks reasonable:

  • there is still a one-time background burst of RPC probe requests on first load
  • but it is now delayed, bounded, and isolated to storage persistence for the next load rather than affecting the current page render path

So from a performance / UX perspective, this looks good to me now. LGTM.

@AugustoL AugustoL merged commit ff1c2a0 into openscan-explorer:dev Mar 9, 2026
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants