smart-order-router
smart-order-router copied to clipboard
Browser is not defined
Bug Description The problem occurs on the widget screen. The endless error "Error fetching trade"
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,
}
},
})
I have this issue with widget as well
i have this issue, when i create new AlphaRouter instance
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
i had the same issue, adding in
in index.html resolve this problem
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 what we did is two steps:
- In the module containing the component that uses the smart-order-router we added:
if (typeof window !== "undefined") {
// @ts-ignore
window.Browser = {
T: () => {
}
};
}
- We made that whole component client-only (using
"use client"
) directive
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
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 auseEffect
gated by aif (typeof window !== "undefined")
conditional and it doesn't change anything![]()
try this version , "@uniswap/smart-order-router": "3.19.3" , that works for me
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>
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 auseEffect
gated by aif (typeof window !== "undefined")
conditional and it doesn't change anythingtry 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.
None of the solutions worked for us, but when we change the version to 3.19.3 then we could use this package
if you on expo or using global.js add globalThis.Browser = { T: () => {} };
to your global.js