This commit is contained in:
parent
5c6ef8366b
commit
072dce8517
|
|
@ -141,6 +141,7 @@ export default async function RootLayout({
|
|||
src="/env.js"
|
||||
strategy="beforeInteractive" // 确保在 React 启动之前加载
|
||||
/> */}
|
||||
<RuntimeEnvProvider /> {/* ✅ 注入 window.RUNTIME_ENV.SUPABASE_URL */}
|
||||
</head>
|
||||
|
||||
<body className={inter.className}>
|
||||
|
|
|
|||
|
|
@ -50,43 +50,75 @@
|
|||
|
||||
|
||||
// app/_runtime-env-provider.tsx (务必保持在 app 目录 ➜ Server 组件)
|
||||
// import { headers } from "next/headers"
|
||||
// import { useServerInsertedHTML } from "next/navigation"
|
||||
// import React from "react"
|
||||
|
||||
// export function RuntimeEnvProvider({ children }: { children: React.ReactNode }) {
|
||||
// /* 解析请求头 */
|
||||
// const h = headers()
|
||||
// console.log("[RuntimeEnv] Raw headers:", Object.fromEntries(h.entries()))
|
||||
|
||||
// const proto =
|
||||
// h.get("x-forwarded-proto") ??
|
||||
// (h.get("host")?.includes(":443") ? "https" : "http")
|
||||
// console.log("[RuntimeEnv] Resolved protocol:", proto)
|
||||
|
||||
// const rawHost = h.get("x-forwarded-host") ?? h.get("host")!
|
||||
// console.log("[RuntimeEnv] rawHost :", rawHost)
|
||||
|
||||
// const hostname = rawHost.split(",")[0].split(":")[0].trim()
|
||||
// console.log("[RuntimeEnv] hostname :", hostname)
|
||||
|
||||
// const supabaseUrl = `${proto}://${hostname}:8000`
|
||||
// console.log("[RuntimeEnv] supabaseUrl to inject:", supabaseUrl)
|
||||
|
||||
// /* 把脚本塞进 HTML(这一行如果不打印,说明没执行) */
|
||||
// useServerInsertedHTML(() => {
|
||||
// console.log("[RuntimeEnv] <script> tag inserted into HTML")
|
||||
// return (
|
||||
// <script
|
||||
// id="runtime-env" /* 方便 View-Source 检查 */
|
||||
// dangerouslySetInnerHTML={{
|
||||
// __html: `window.RUNTIME_ENV = { SUPABASE_URL: ${JSON.stringify(
|
||||
// supabaseUrl,
|
||||
// )} }; console.log('[Client] window.RUNTIME_ENV =', window.RUNTIME_ENV);`,
|
||||
// }}
|
||||
// />
|
||||
// )
|
||||
// })
|
||||
|
||||
// return <>{children}</>
|
||||
// }
|
||||
|
||||
|
||||
// components/utility/runtime-env-provider.tsx
|
||||
import { headers } from "next/headers"
|
||||
import { useServerInsertedHTML } from "next/navigation"
|
||||
import React from "react"
|
||||
|
||||
export function RuntimeEnvProvider({ children }: { children: React.ReactNode }) {
|
||||
/* 解析请求头 */
|
||||
export function RuntimeEnvProvider() {
|
||||
const h = headers()
|
||||
console.log("[RuntimeEnv] Raw headers:", Object.fromEntries(h.entries()))
|
||||
|
||||
const proto =
|
||||
h.get("x-forwarded-proto") ??
|
||||
(h.get("host")?.includes(":443") ? "https" : "http")
|
||||
console.log("[RuntimeEnv] Resolved protocol:", proto)
|
||||
|
||||
h.get("x-forwarded-proto") ?? (h.get("host")?.includes(":443") ? "https" : "http")
|
||||
const rawHost = h.get("x-forwarded-host") ?? h.get("host")!
|
||||
console.log("[RuntimeEnv] rawHost :", rawHost)
|
||||
|
||||
const hostname = rawHost.split(",")[0].split(":")[0].trim()
|
||||
console.log("[RuntimeEnv] hostname :", hostname)
|
||||
|
||||
const supabaseUrl = `${proto}://${hostname}:8000`
|
||||
console.log("[RuntimeEnv] supabaseUrl to inject:", supabaseUrl)
|
||||
|
||||
/* 把脚本塞进 HTML(这一行如果不打印,说明没执行) */
|
||||
useServerInsertedHTML(() => {
|
||||
console.log("[RuntimeEnv] <script> tag inserted into HTML")
|
||||
return (
|
||||
<script
|
||||
id="runtime-env" /* 方便 View-Source 检查 */
|
||||
id="runtime-env"
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `window.RUNTIME_ENV = { SUPABASE_URL: ${JSON.stringify(
|
||||
supabaseUrl,
|
||||
)} }; console.log('[Client] window.RUNTIME_ENV =', window.RUNTIME_ENV);`,
|
||||
supabaseUrl
|
||||
)} };`,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
})
|
||||
|
||||
return <>{children}</>
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue