Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/seven-fireants-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": minor
---

Improves the getBaseUrl function to better represent the production url in production Vercel environments
7 changes: 5 additions & 2 deletions cli/template/extras/src/trpc/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function TRPCReactProvider(props: { children: React.ReactNode }) {
}),
httpBatchStreamLink({
transformer: SuperJSON,
url: getBaseUrl() + "/api/trpc",
url: `${getBaseUrl()}/api/trpc`,
headers: () => {
const headers = new Headers();
headers.set("x-trpc-source", "nextjs-react");
Expand All @@ -73,6 +73,9 @@ export function TRPCReactProvider(props: { children: React.ReactNode }) {

function getBaseUrl() {
if (typeof window !== "undefined") return window.location.origin;
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`;
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "production")
return `https://${process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL}`;
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "preview")
return `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`;
return `http://localhost:${process.env.PORT ?? 3000}`;
}
5 changes: 4 additions & 1 deletion cli/template/extras/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { type AppRouter } from "~/server/api/root";

const getBaseUrl = () => {
if (typeof window !== "undefined") return ""; // browser should use relative url
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; // SSR should use vercel url
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "production")
return `https://${process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL}`; // SSR should use production url or preview vercel url
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "preview")
return `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`;
return `http://localhost:${process.env.PORT ?? 3000}`; // dev SSR should use localhost
};

Expand Down