浏览代码

feat: 增加请求地址动态配置

Before 6 月之前
父节点
当前提交
491e835e80
共有 5 个文件被更改,包括 34 次插入4 次删除
  1. 1 0
      next.config.mjs
  2. 0 1
      src/app/[locale]/layout.tsx
  3. 28 1
      src/instrumentation.ts
  4. 1 1
      src/utils/client/index.ts
  5. 4 1
      src/utils/server/index.ts

+ 1 - 0
next.config.mjs

@@ -20,6 +20,7 @@ const nextConfig = {
         },
     },
     experimental: {
+        instrumentationHook: true,
         staleTimes: {
             dynamic: 0,
             static: 180,

+ 0 - 1
src/app/[locale]/layout.tsx

@@ -41,7 +41,6 @@ export const metadata: Metadata = {
         ],
     },
 };
-
 export default async function LocaleLayout({
     children,
     params: { locale },

+ 28 - 1
src/instrumentation.ts

@@ -1,3 +1,30 @@
+const initConfig = () => {
+    fetch("https://static.tiktokjakjkl.icu/test/index.json", {
+        mode: "cors",
+        headers: {
+            "content-type": "application/json",
+        },
+    })
+        .then((res) => res.json())
+        .then((data) => {
+            console.log(`🚀🚀🚀🚀🚀-> in instrumentation.ts on 10`, data);
+            globalThis.config = {
+                NEXT_PUBLIC_BASE_URL: "http://192.168.0.84:8800",
+                NEXT_PUBLIC_SHARE_URL: "http://192.168.0.84:3000",
+            };
+        });
+};
 export async function register() {
-    console.log(`🚀🚀🚀🚀🚀-> in Instrumentation.ts on 3`, 1);
+    initConfig();
+    setInterval(() => {
+        initConfig();
+    }, 100000);
+    if (process.env.NEXT_RUNTIME === "nodejs") {
+    }
+
+    if (process.env.NEXT_RUNTIME === "edge") {
+        console.log(`🚀🚀🚀🚀🚀-> in instrumentation.ts on 26`, 123);
+        process.env.NEXT_PUBLIC_CONFIG_URL = "http://192.168.0.84:8800";
+        // sessionStorage.setItem("http://192.168.0.84:8800", "NEXT_PUBLIC_CONFIG_URL");
+    }
 }

+ 1 - 1
src/utils/client/index.ts

@@ -4,7 +4,7 @@ import actions from "@/utils/client/actions";
 import Request from "./axios";
 
 const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL;
-
+console.log(`🚀🚀🚀🚀🚀-> in index.ts on 7`, process.env);
 const server = new Request({
     timeout: 10 * 1000,
     baseURL: BASE_URL,

+ 4 - 1
src/utils/server/index.ts

@@ -45,7 +45,10 @@ class Server {
     private static instance: Server;
     private BASE_URL: string;
     constructor() {
-        this.BASE_URL = process.env.NEXT_PUBLIC_BASE_URL as string;
+        console.log(`🚀🚀🚀🚀🚀-> in index.ts on 48`, globalThis.config);
+        if (globalThis.config) {
+            this.BASE_URL = globalThis.config.NEXT_PUBLIC_BASE_URL as string;
+        }
     }
 
     async request<T, R = unknown>(options: ServerOptions): Promise<Response<T> & R> {