parent
9b61cb1335
commit
d6e6dd09f0
@ -0,0 +1,21 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
import { getServerSideConfig } from "../../config/server";
|
||||||
|
|
||||||
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
|
// Danger! Don not write any secret value here!
|
||||||
|
// 警告!不要在这里写入任何敏感信息!
|
||||||
|
const DANGER_CONFIG = {
|
||||||
|
needCode: serverConfig.needCode,
|
||||||
|
};
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
type DangerConfig = typeof DANGER_CONFIG;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(req: NextRequest) {
|
||||||
|
return NextResponse.json({
|
||||||
|
needCode: serverConfig.needCode,
|
||||||
|
});
|
||||||
|
}
|
@ -1,42 +0,0 @@
|
|||||||
import { RUNTIME_CONFIG_DOM } from "../constant";
|
|
||||||
|
|
||||||
function queryMeta(key: string, defaultValue?: string): string {
|
|
||||||
let ret: string;
|
|
||||||
if (document) {
|
|
||||||
const meta = document.head.querySelector(
|
|
||||||
`meta[name='${key}']`,
|
|
||||||
) as HTMLMetaElement;
|
|
||||||
ret = meta?.content ?? "";
|
|
||||||
} else {
|
|
||||||
ret = defaultValue ?? "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getClientSideConfig() {
|
|
||||||
if (typeof window === "undefined") {
|
|
||||||
throw Error(
|
|
||||||
"[Client Config] you are importing a browser-only module outside of browser",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const dom = document.getElementById(RUNTIME_CONFIG_DOM);
|
|
||||||
|
|
||||||
if (!dom) {
|
|
||||||
throw Error("[Config] Dont get config before page loading!");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const fromServerConfig = JSON.parse(dom.innerText) as DangerConfig;
|
|
||||||
const fromBuildConfig = {
|
|
||||||
version: queryMeta("version"),
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
...fromServerConfig,
|
|
||||||
...fromBuildConfig,
|
|
||||||
};
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Config] failed to parse client config");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue