smart-order-router icon indicating copy to clipboard operation
smart-order-router copied to clipboard

Browser is not defined

Open HarlamovEvgeniy opened this issue 1 year ago • 12 comments

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,
    }
  },
})

HarlamovEvgeniy avatar Feb 02 '24 00:02 HarlamovEvgeniy

I have this issue with widget as well

Meow711 avatar Feb 02 '24 02:02 Meow711

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

Yan-Doshchinskiy avatar Feb 04 '24 08:02 Yan-Doshchinskiy

I've added a workaround in this comment, though I belive a proper solution should be found: https://github.com/Uniswap/widgets/issues/627#issuecomment-1930627298

Wunsz avatar Feb 06 '24 19:02 Wunsz

i had the same issue, adding in

in index.html resolve this problem

axer4 avatar Feb 12 '24 11:02 axer4

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?

LilNait-S avatar Feb 23 '24 03:02 LilNait-S

@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

Wunsz avatar Feb 23 '24 09:02 Wunsz

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

c0rv0s avatar Mar 17 '24 20:03 c0rv0s

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

LilNait-S avatar Mar 17 '24 20:03 LilNait-S

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>

0x-sen avatar Mar 27 '24 12:03 0x-sen

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.

IAmKio avatar Mar 28 '24 12:03 IAmKio

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

alicercedigital avatar Apr 03 '24 21:04 alicercedigital

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

garantor avatar Jun 21 '24 13:06 garantor