Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions apps/insights/src/utils/absolute-url.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { headers } from "next/headers";

import {
demand,
IS_PREVIEW_SERVER,
IS_PRODUCTION_SERVER,
} from "../config/server";

/**
* Returns an absolute URL for the given pathname.
*
Expand All @@ -30,20 +24,11 @@ export async function absoluteUrl(pathname: string) {
// if we have a host and a proto, we can construct the origin
if (host && proto) origin = `${proto}://${host}`;
} catch {
// headers() is unavailable
throw new Error("Headers are not available in this context. Please check where are you calling this function from.");
}

// Fallbacks for requests where headers() is not available
if (!origin) {
if (IS_PRODUCTION_SERVER) {
const productionUrl = demand("VERCEL_PROJECT_PRODUCTION_URL");
origin = `https://${productionUrl}`;
} else if (IS_PREVIEW_SERVER) {
const previewUrl = demand("VERCEL_URL");
origin = `https://${previewUrl}`;
} else {
origin = "http://localhost:3003";
}
throw new Error("No origin found! This should never happen.");
}

const path = pathname.startsWith("/") ? pathname : `/${pathname}`;
Expand Down
Loading