Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add provider dynamic import example #282

Closed

Conversation

albertpurnama
Copy link

Your ENS/address: apurn.eth

I moved Provider away from _app.tsx to demonstrate how to dynamically load wagmi's Provider.

This serves as an answer for #32.

Improvements

Before:
Screen Shot 2022-03-25 at 12 39 26 AM
First Load JS >300kb across all pages

After:
Screen Shot 2022-03-25 at 12 44 17 AM
First load JS on heaviest page (the one where we import wagmi without any lazy loading) = 200kb
Shared JS (~70kb) because we dynamically loaded wagmi AND ethers

@changeset-bot
Copy link

changeset-bot bot commented Mar 25, 2022

⚠️ No Changeset found

Latest commit: a4b569a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Mar 25, 2022

Someone is attempting to deploy a commit to a Personal Account owned by @tmm on Vercel.

@tmm first needs to authorize it.

@albertpurnama albertpurnama changed the title Add provider dynamic import Add provider dynamic import example Mar 25, 2022
@vercel
Copy link

vercel bot commented Mar 27, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/zoo/wagmi/7GGfUXkD9tLCeYs48GRTs8UA85i7
✅ Preview: https://wagmi-git-fork-albertputrapurnama-example-dynamic-loading-zoo.vercel.app

@tmm
Copy link
Member

tmm commented Mar 30, 2022

Thanks for doing this! Let's keep the example in GitHub Discussions for now.

@tmm tmm closed this Mar 30, 2022
@LaCocoRoco
Copy link

LaCocoRoco commented Jul 18, 2022

Thanks to this issue i could solve my problem with a similiar approach.
Probably there have been some changes to NextJS and Wagmi.
I will post it as reference for future use.

// components/WagmiProvider.tsx

import { ReactNode } from 'react';
import { createClient, WagmiConfig } from 'wagmi';

import { getDefaultProvider } from 'ethers';

const client = createClient({
  autoConnect: true,
  provider: getDefaultProvider(),
});

const WagmiProvider = ({ children }: { children: ReactNode }) => {
  return <WagmiConfig client={client}>{children}</WagmiConfig>;
};

export default WagmiProvider;
// _app.tsx

import type { AppProps } from 'next/app';
import dynamic from 'next/dynamic';

const WagmiProvider = dynamic(() => import('components/WagmiProvider'));

const App = ({ Component, pageProps }: AppProps) => {
  return (
    <>
      <WagmiProvider>
        <Component {...pageProps} />
      </WagmiProvider>
    </>
  );
};

export default App;

@seeARMS
Copy link

seeARMS commented Sep 13, 2022

@LaCocoRoco, it seems your solution doesn't actually cause the WagmiProvider to be dynamically imported. Perhaps it's due to the reasons mentioned in #32.

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.

4 participants