Skip to content

Conversation

@nahom4
Copy link

@nahom4 nahom4 commented Dec 29, 2025

[FEAT] WASM: Implement Cached Initialization to Skip 0DNS Calls on Refresh

Summary

This PR introduces a caching mechanism for the WASM SDK to significantly improve initialization performance on page reloads. By storing network details (miners and sharders) in the browser's localStorage, the SDK can now initialize instantly without making a network call to the 0DNS (block worker) server.

Changes

Core Logic

  • core/conf/config.go: Added Miners and Sharders fields to the Config struct to allow passing pre-defined network details.
  • core/client/init_node.go: Refactored the Init and initNode functions to skip network discovery if Miners and Sharders are provided in the configuration.
  • core/client/set.go: Updated InitSDKWithWebApp to pass these cached network details into the core initialization flow.

WASM Layer

  • wasmsdk/sdk.go:
    • Implemented loadNetworkFromCache and saveNetworkToCache using the browser's localStorage via syscall/js.
    • Updated initSDKs to accept a new refresh (boolean) parameter.
    • Logic: If refresh is false, it attempts to load from cache; if the cache is empty or refresh is true, it performs a standard network discovery and updates the cache.
  • wasmsdk/proxy.go: Updated the JavaScript binding for the init function to accommodate the new refresh parameter.

Impact & Safety

  • Web App: Page refreshes now initialize the SDK significantly faster with zero network overhead.
  • Mobile & Desktop: Zero impact. The existing InitSDK entry point remains untouched, and the core logic gracefully falls back to standard discovery when cached fields are empty.
  • Backward Compatibility: The new field additions to the Config struct are non-breaking.

How to Test

  1. Initial Load: Observe a network request to the block worker's /network endpoint. Verify zcn_network_info is created in localStorage.
  2. Page Refresh: Verify that the /network call is not made, and the SDK initializes successfully using the cache.
  3. Forced Refresh: Call sdk.init(..., true) from JS. Verify that the SDK bypasses the cache and hits the network.

Checklist

  • Implemented localStorage caching logic.
  • Added refresh flag support.
  • Optimized core/client to skip redundant DNS calls.
  • Verified zero impact on legacy InitSDK path.

@nahom4 nahom4 requested a review from guruhubb as a code owner December 29, 2025 21:52
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.

2 participants