Last active
April 26, 2025 10:15
-
-
Save hellokaton/8c9157655599b3318919e1cbe33277b9 to your computer and use it in GitHub Desktop.
解决国内 nextjs 三方登录等网络问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isDev = process.env.NODE_ENV === "development"; | |
if (isDev) { | |
require("./proxy-setup"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ProxyAgent, Dispatcher } from "undici"; | |
const { fetch: originalFetch } = global; | |
const proxyAgent = new ProxyAgent("http://127.0.0.1:7890/"); | |
global.fetch = ( | |
url: string | URL | Request, | |
opts: RequestInit & { dispatcher?: Dispatcher } = {} | |
) => { | |
// 只对特定域名使用代理 | |
// const urlStr = url.toString(); | |
// 排除本地 API 请求 | |
// if (urlStr.includes(":3008/api/")) { | |
// return originalFetch(url, opts); | |
//} | |
// 请求使用代理 | |
opts.dispatcher = proxyAgent; | |
try { | |
return originalFetch(url, opts); | |
} catch (error) { | |
console.error("Fetch error:", error); | |
throw error; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment