feat: improve handling of deployment env vars
These are now defined in Netlify UI for dedicated environments (test, stage, production): AUTH_URL NEXTAUTH_URL PUBLIC_URL Code now falls back to incoming request host. Mainly used for deployment previews which do not have Akamai in front, meaning we do not need the above workaround as incoming request host matches the actual public facing host. When Akamai is in front, we lose the public facing host in Netlify's routing layer as they internally use `x-forwarded-for` and we can't claim it for our usage.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { env } from "@/env/server"
|
||||
import * as api from "@/lib/api"
|
||||
@@ -9,7 +10,9 @@ import type { FriendTransaction, Stay } from "./output"
|
||||
|
||||
const meter = metrics.getMeter("trpc.user")
|
||||
const getProfileCounter = meter.createCounter("trpc.user.profile")
|
||||
const getProfileSuccessCounter = meter.createCounter("trpc.user.profile-success")
|
||||
const getProfileSuccessCounter = meter.createCounter(
|
||||
"trpc.user.profile-success"
|
||||
)
|
||||
const getProfileFailCounter = meter.createCounter("trpc.user.profile-fail")
|
||||
|
||||
async function updateStaysBookingUrl(
|
||||
@@ -41,7 +44,7 @@ async function updateStaysBookingUrl(
|
||||
// Temporary Url, domain and lang support for current web
|
||||
const bookingUrl = new URL(
|
||||
"/hotelreservation/my-booking",
|
||||
env.PUBLIC_URL ?? ""
|
||||
env.PUBLIC_URL || "https://www.scandichotels.com" // fallback to production for ephemeral envs (like deploy previews)
|
||||
)
|
||||
switch (lang) {
|
||||
case Lang.sv:
|
||||
@@ -83,10 +86,7 @@ async function updateStaysBookingUrl(
|
||||
}
|
||||
|
||||
getProfileSuccessCounter.add(1)
|
||||
console.info(
|
||||
"api.user.profile updatebookingurl success",
|
||||
JSON.stringify({})
|
||||
)
|
||||
console.info("api.user.profile updatebookingurl success", JSON.stringify({}))
|
||||
|
||||
return data.map((d) => {
|
||||
const originalString =
|
||||
@@ -98,10 +98,7 @@ async function updateStaysBookingUrl(
|
||||
bookingUrl.searchParams.set("RefId", encryptedBookingValue)
|
||||
} else {
|
||||
bookingUrl.searchParams.set("lastName", apiJson.data.attributes.lastName)
|
||||
bookingUrl.searchParams.set(
|
||||
"bookingId",
|
||||
d.attributes.confirmationNumber
|
||||
)
|
||||
bookingUrl.searchParams.set("bookingId", d.attributes.confirmationNumber)
|
||||
}
|
||||
return {
|
||||
...d,
|
||||
@@ -113,4 +110,4 @@ async function updateStaysBookingUrl(
|
||||
})
|
||||
}
|
||||
|
||||
export { updateStaysBookingUrl }
|
||||
export { updateStaysBookingUrl }
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { env } from "@/env/server"
|
||||
|
||||
import type { NextRequest } from "next/server"
|
||||
|
||||
export const langInput = z.object({
|
||||
lang: z.nativeEnum(Lang),
|
||||
@@ -33,3 +36,27 @@ export function toLang(lang: string): Lang | undefined {
|
||||
const lowerCaseLang = lang.toLowerCase()
|
||||
return Object.values(Lang).find((l) => l === lowerCaseLang)
|
||||
}
|
||||
|
||||
export function getPublicURL(request: NextRequest) {
|
||||
if (env.PUBLIC_URL) {
|
||||
return env.PUBLIC_URL
|
||||
}
|
||||
|
||||
const host = request.nextUrl.host
|
||||
const proto = request.nextUrl.protocol
|
||||
return `${proto}//${host}`
|
||||
}
|
||||
|
||||
export function getPublicNextURL(request: NextRequest) {
|
||||
if (env.PUBLIC_URL) {
|
||||
const publicNextURL = request.nextUrl.clone()
|
||||
// Akamai in front of Netlify for dedicated environments
|
||||
// require us to rewrite the incoming host and hostname
|
||||
// to match the public URL used to visit Akamai.
|
||||
const url = new URL(env.PUBLIC_URL)
|
||||
publicNextURL.host = url.host
|
||||
publicNextURL.hostname = url.hostname
|
||||
return publicNextURL
|
||||
}
|
||||
return request.nextUrl
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user