import { env } from "@/env/server" import type { NextRequest } from "next/server" /** * Use this function when you want to create URLs that are public facing, for * example for redirects or redirectTo query parameters. * Dedicated environments are behind Akamai (test, stage, production). They have * env.PUBLIC_URL set. * All other environment like deploy previews and branch deployments are not * behind Akamai and therefore do not have env.PUBLIC_URL set. * We need this approach because Netlify uses x-forwarded-host internally and * strips it from ever reaching our code. * TODO: Replace this approach with custom header in Akamai that mirrors the * value in x-forwarded-host which would not get stripped by Netlify. * @param request The incoming request. * @returns NextURL The public facing URL instance for the given request. */ 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 } /** * Use this function when you want the public facing URL for the given request. * Read about the motivation in getPublicNextURL above. * @see getPublicNextURL * @param request The incoming request. * @returns string The public facing origin for the given request. */ export function getPublicURL(request: NextRequest) { if (env.PUBLIC_URL) { return env.PUBLIC_URL } return request.nextUrl.origin }