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

Browser is not defined #484

Open
HarlamovEvgeniy opened this issue Feb 2, 2024 · 13 comments
Open

Browser is not defined #484

HarlamovEvgeniy opened this issue Feb 2, 2024 · 13 comments

Comments

@HarlamovEvgeniy
Copy link

Bug Description
The problem occurs on the widget screen. The endless error "Error fetching trade"

image

I have this problem both when using @uniswap/widgets and when writing my widget via @uniswap/v3-sdk. I created a similar issue on the @uniswap/widgets project, but decided to duplicate it here, maybe someone has come across this.

I assume that the problem may lie in the collector and the absence of some polyphiles. But I use Vite with this vite,config.ts

export default defineConfig({
  plugins: [
    react(),
    createHtmlPlugin({
      minify: true,
      entry: '/src/main.tsx',
      template: '/index.html',
    }),
    svgLoader(),
    chunkSplitPlugin({
      strategy: 'unbundle',
    }),
    nodePolyfills()
  ],
  server: {
    port: 3000,
    open: true,
  },
  optimizeDeps: {
    esbuildOptions: {
      define: {
        global: 'globalThis'
      }
    }
  },
  resolve: {
    alias: {
      "@": fileURLToPath(new URL('./src', import.meta.url)),
      jsbi: path.resolve(__dirname, "./node_modules/jsbi/dist/jsbi-cjs.js"),
      process: "process/browser",
      buffer: "buffer",
      crypto: "crypto-browserify",
      stream: "stream-browserify",
      assert: "assert",
      http: "stream-http",
      https: "https-browserify",
      os: "os-browserify",
      url: "url",
      util: "util",
    }
  },
  build: {
    outDir: path.join(__dirname, "build"),
    sourcemap: false,
    minify: true,
    commonjsOptions: {
      transformMixedEsModules: true,
    }
  },
})
@Meow711
Copy link

Meow711 commented Feb 2, 2024

I have this issue with widget as well

@Yan-Doshchinskiy
Copy link

i have this issue, when i create new AlphaRouter instance
image

@Wunsz
Copy link

Wunsz commented Feb 6, 2024

I've added a workaround in this comment, though I belive a proper solution should be found:
Uniswap/widgets#627 (comment)

@axer4
Copy link

axer4 commented Feb 12, 2024

i had the same issue, adding <script>
globalThis.Browser = { T: () => {} };
</script> in in index.html resolve this problem

@LilNait-S
Copy link

Agregué una solución alternativa en este comentario, aunque creo que se debe encontrar una solución adecuada: Uniswap/widgets#627 (comentario)

How could I solve it in Next.js?

@Wunsz
Copy link

Wunsz commented Feb 23, 2024

@LilNait-S what we did is two steps:

  1. In the module containing the component that uses the smart-order-router we added:
if (typeof window !== "undefined") {
// @ts-ignore
  window.Browser = {
    T: () => {
    }
  };
}
  1. We made that whole component client-only (using "use client") directive

@c0rv0s
Copy link

c0rv0s commented Mar 17, 2024

I'm seeing the same thing when trying to import the AlphaRouter into a next js project. I've tried adding the 'use client' directive to the component and also only instantiating the class inside a useEffect gated by a if (typeof window !== "undefined") conditional and it doesn't change anything

Screenshot 2024-03-17 at 3 11 14 PM

@LilNait-S
Copy link

I'm seeing the same thing when trying to import the AlphaRouter into a next js project. I've tried adding the 'use client' directive to the component and also only instantiating the class inside a useEffect gated by a if (typeof window !== "undefined") conditional and it doesn't change anything

Screenshot 2024-03-17 at 3 11 14 PM

try this version ,
"@uniswap/smart-order-router": "3.19.3" ,
that works for me

@0x-sen
Copy link

0x-sen commented Mar 27, 2024

a next js project can add a <Script/> in _document.tsx

import Script from "next/script";

<Script strategy="beforeInteractive" id="test">
      {`globalThis.Browser = { T: () => {} };`}
 </Script>

@IAmKio
Copy link

IAmKio commented Mar 28, 2024

I'm seeing the same thing when trying to import the AlphaRouter into a next js project. I've tried adding the 'use client' directive to the component and also only instantiating the class inside a useEffect gated by a if (typeof window !== "undefined") conditional and it doesn't change anything

try this version , "@uniswap/smart-order-router": "3.19.3" , that works for me

Worth noting that 3.19.3 worked for us too, but later versions regressed.

@alicercedigital
Copy link

None of the solutions worked for us, but when we change the version to 3.19.3 then we could use this package

@garantor
Copy link

if you on expo or using global.js add globalThis.Browser = { T: () => {} }; to your global.js

@javaLuo
Copy link

javaLuo commented Nov 28, 2024

I'm seeing the same thing when trying to import the AlphaRouter into a next js project. I've tried adding the 'use client' directive to the component and also only instantiating the class inside a useEffect gated by a if (typeof window !== "undefined") conditional and it doesn't change anything
Screenshot 2024-03-17 at 3 11 14 PM

try this version , "@uniswap/smart-order-router": "3.19.3" , that works for me

Have you solved this problem?
“ReferenceError: Browser is not defined”, I have the same issue

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

No branches or pull requests