Skip to content

Instantly share code, notes, and snippets.

@hellokaton
Last active April 26, 2025 10:15
Show Gist options
  • Save hellokaton/8c9157655599b3318919e1cbe33277b9 to your computer and use it in GitHub Desktop.
Save hellokaton/8c9157655599b3318919e1cbe33277b9 to your computer and use it in GitHub Desktop.
解决国内 nextjs 三方登录等网络问题
const isDev = process.env.NODE_ENV === "development";
if (isDev) {
require("./proxy-setup");
}
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