fix: debug vercel headers

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei 2024-10-23 19:51:03 +08:00
parent 1e695ded93
commit 29a48eed48
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
2 changed files with 42 additions and 36 deletions

View File

@ -34,7 +34,7 @@ export async function injectMetaHandler(req: FastifyRequest): Promise<MetaTag[]>
const apiClient = createApiClient()
const hostFromReq = req.headers.host
const hostFromReq = req.headers["x-forwarded-host"] || req.headers.host
const protocol = req.headers["x-forwarded-proto"] || req.protocol || "https"
const url = req.originalUrl

View File

@ -33,39 +33,45 @@ const devHandler = (app: FastifyInstance) => {
})
}
const prodHandler = (app: FastifyInstance) => {
app.get("/share/test-vercel-header", (req, reply) => {
reply.send({
headers: req.headers,
host: req.headers.host,
})
})
app.get("*", async (req, reply) => {
let template = require("../../.generated/index.template").default
template = await safeInjectMetaToTemplate(template, req, reply)
const isInVercelReverseProxy = req.headers["x-middleware-subrequest"]
// const isInVercelReverseProxy = req.headers["x-middleware-subrequest"]
if (isInVercelReverseProxy) {
// Add asset prefix
// Map all link and script to /assets
// <script type="module" crossorigin src="/assets/index-kQ31_hj7.js"></script>
// <link rel="stylesheet" crossorigin href="/assets/index-BORNdGZP.css">
const prefix = "/external-dist"
const { document } = parseHTML(template)
for (const script of document.querySelectorAll("script")) {
script.src = `${prefix}${script.src}`
}
for (const link of document.querySelectorAll("link")) {
link.href = `${prefix}${link.href}`
}
// if (isInVercelReverseProxy) {
// Add asset prefix
// Map all link and script to /assets
// <script type="module" crossorigin src="/assets/index-kQ31_hj7.js"></script>
// <link rel="stylesheet" crossorigin href="/assets/index-BORNdGZP.css">
const prefix = "/external-dist"
const { document } = parseHTML(template)
for (const script of document.querySelectorAll("script")) {
script.src = `${prefix}${script.src}`
}
for (const link of document.querySelectorAll("link")) {
link.href = `${prefix}${link.href}`
}
const upstreamEnv = req.requestContext.get("upstreamEnv")
const upstreamEnv = req.requestContext.get("upstreamEnv")
if (upstreamEnv) {
document.head.prepend(document.createComment(`upstreamEnv: ${upstreamEnv}`))
if (upstreamEnv) {
document.head.prepend(document.createComment(`upstreamEnv: ${upstreamEnv}`))
// override client side api url
// override client side api url
const upstreamHost = req.headers["x-forwarded-host"] || req.headers.host
const protocol = req.headers["x-forwarded-proto"] || "https"
const upstreamOrigin = `${protocol}://${upstreamHost}`
const upstreamHost = req.headers["x-forwarded-host"] || req.headers.host
const protocol = req.headers["x-forwarded-proto"] || "https"
const upstreamOrigin = `${protocol}://${upstreamHost}`
const injectScript = (apiUrl: string) => {
const template = `function injectEnv(env2) {
const injectScript = (apiUrl: string) => {
const template = `function injectEnv(env2) {
for (const key in env2) {
if (env2[key] === void 0) continue;
globalThis["__followEnv"] ??= {};
@ -73,21 +79,21 @@ const prodHandler = (app: FastifyInstance) => {
}
}
injectEnv({"VITE_API_URL":"${apiUrl}","VITE_EXTERNAL_API_URL":"${apiUrl}","VITE_WEB_URL":"${upstreamOrigin}"})`
const $script = document.createElement("script")
$script.innerHTML = template
document.head.prepend($script)
}
if (upstreamEnv === "dev" && env.VITE_EXTERNAL_DEV_API_URL) {
injectScript(env.VITE_EXTERNAL_DEV_API_URL)
}
if (upstreamEnv === "prod" && env.VITE_EXTERNAL_PROD_API_URL) {
injectScript(env.VITE_EXTERNAL_PROD_API_URL)
}
const $script = document.createElement("script")
$script.innerHTML = template
document.head.prepend($script)
}
if (upstreamEnv === "dev" && env.VITE_EXTERNAL_DEV_API_URL) {
injectScript(env.VITE_EXTERNAL_DEV_API_URL)
}
if (upstreamEnv === "prod" && env.VITE_EXTERNAL_PROD_API_URL) {
injectScript(env.VITE_EXTERNAL_PROD_API_URL)
}
template = document.toString()
}
template = document.toString()
// }
reply.type("text/html")
reply.send(template)
})