Uniswap / smart-order-router

GNU General Public License v3.0
401 stars 400 forks source link

Browser is not defined #484

Open HarlamovEvgeniy opened 7 months ago

HarlamovEvgeniy commented 7 months ago

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 commented 7 months ago

I have this issue with widget as well

Yan-Doshchinskiy commented 7 months ago

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

Wunsz commented 7 months ago

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

axer4 commented 7 months ago

i had the same issue, adding in in index.html resolve this problem

LilNait-S commented 6 months ago

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 commented 6 months ago

@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: () => {
    }
    };
    }
  2. We made that whole component client-only (using "use client") directive

c0rv0s commented 6 months ago

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 commented 6 months ago

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 commented 5 months ago

a next js project can add a Githubissues.

  • Githubissues is a development platform for aggregating issues.