question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

chainChanged emits on page load

See original GitHub issue

Describe the bug Sometimes chainChanged message emits on every page load. So if I have this line in my code: ethereum.on('chainChanged', (_chainId) => window.location.reload()); then user gets endless page refresh loop.

To Reproduce I couldn’t reproduce this, but one of our user shared his screen and I saw this endless page refresh loop. I added console.log('chainChanged'); to that event callback, and console showed this log every time before page refreshed.

Expected behavior chainChanged event should be sent only on chain change, not on page load

Browser details

  • OS: OS X
  • Browser: last Chrome
  • MetaMask Version: 8.0.8

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mqklincommented, Nov 19, 2020

@mehranhydary to fix this I store lastChainId in localStorage and on chainChained check if it’s the same:

      (async () => {
        try {
          const chainId = await web3.eth.getChainId();
          StoredLastEthereumChain.set(`0x${chainId.toString(16)}`); // save to localStorage

          web3.currentProvider.on(
            'chainChanged',
            chainId => {
              const lastChainId = StoredLastEthereumChain.get(); // get from localStorage
              if (String(chainId) !== String(lastChainId)) {
                window.location.reload();
              }
            },
          );
        }
        catch (e) {
          // handle exception
        }
      })();
0reactions
laygircommented, Jan 25, 2022

I’m having this issue with Brave but not with Chrome.

Brave v1.34.81 Chromium: 97.0.4692.99 MetaMask v10.8.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ethereum Provider API - MetaMask Docs
This means that accountsChanged will be emitted whenever the user's exposed account address changes. Tip. We plan to allow the eth_accounts ...
Read more >
How to emit chainId changed in flutter dapp browser
Good evening, in my dapp browser app, when the user requests a chain id change, i use to reload the page, but i...
Read more >
How Can You Find the Ethereum Provider on Web3 Browsers ...
addEventListener('load', async function() { if (typeof ethereum !== 'undefined') { // Supports EIP-1102 injected Ethereum providers.
Read more >
EIP-1193: Ethereum Provider JavaScript API
The Provider emits chainChanged when connecting to a new chain. ... Most Providers are available as window.ethereum on page load.
Read more >
abcwallet-extension-provider-api - npm
ABC Wallet injects a global API into website visited by its users at `window.abc`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found