From 77035c93627c6f966029f4cc01ee8c5fea819a0b Mon Sep 17 00:00:00 2001 From: valiant1x <29789261+valiant1x@users.noreply.github.com> Date: Wed, 17 Apr 2024 10:22:33 -0500 Subject: [PATCH] refactor baseUrl to consider vercel prod env and existing env var for prod URL (TWITTER_SITE) --- app/layout.tsx | 6 ++---- app/robots.ts | 8 +++----- app/sitemap.ts | 13 +++++-------- lib/constants.ts | 7 +++++++ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/layout.tsx b/app/layout.tsx index 58f5a9708e..3471a5865a 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -3,16 +3,14 @@ import { GeistSans } from 'geist/font'; import { ensureStartsWith } from 'lib/utils'; import { ReactNode, Suspense } from 'react'; import './globals.css'; +import { BASE_URL } from 'lib/constants'; const { TWITTER_CREATOR, TWITTER_SITE, SITE_NAME } = process.env; -const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL - ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` - : 'http://localhost:3000'; const twitterCreator = TWITTER_CREATOR ? ensureStartsWith(TWITTER_CREATOR, '@') : undefined; const twitterSite = TWITTER_SITE ? ensureStartsWith(TWITTER_SITE, 'https://') : undefined; export const metadata = { - metadataBase: new URL(baseUrl), + metadataBase: new URL(BASE_URL), title: { default: SITE_NAME!, template: `%s | ${SITE_NAME}` diff --git a/app/robots.ts b/app/robots.ts index c9849a2762..0c806e3644 100644 --- a/app/robots.ts +++ b/app/robots.ts @@ -1,6 +1,4 @@ -const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL - ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` - : 'http://localhost:3000'; +import { BASE_URL } from "lib/constants"; export default function robots() { return { @@ -9,7 +7,7 @@ export default function robots() { userAgent: '*' } ], - sitemap: `${baseUrl}/sitemap.xml`, - host: baseUrl + sitemap: `${BASE_URL}/sitemap.xml`, + host: BASE_URL }; } diff --git a/app/sitemap.ts b/app/sitemap.ts index fe8ed96ac2..0a7f5fb13d 100644 --- a/app/sitemap.ts +++ b/app/sitemap.ts @@ -1,3 +1,4 @@ +import { BASE_URL } from 'lib/constants'; import { getCollections, getPages, getProducts } from 'lib/shopify'; import { validateEnvironmentVariables } from 'lib/utils'; import { MetadataRoute } from 'next'; @@ -7,35 +8,31 @@ type Route = { lastModified: string; }; -const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL - ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` - : 'http://localhost:3000'; - export default async function sitemap(): Promise { validateEnvironmentVariables(); const routesMap = [''].map((route) => ({ - url: `${baseUrl}${route}`, + url: `${BASE_URL}${route}`, lastModified: new Date().toISOString() })); const collectionsPromise = getCollections().then((collections) => collections.map((collection) => ({ - url: `${baseUrl}${collection.path}`, + url: `${BASE_URL}${collection.path}`, lastModified: collection.updatedAt })) ); const productsPromise = getProducts({}).then((products) => products.map((product) => ({ - url: `${baseUrl}/product/${product.handle}`, + url: `${BASE_URL}/product/${product.handle}`, lastModified: product.updatedAt })) ); const pagesPromise = getPages().then((pages) => pages.map((page) => ({ - url: `${baseUrl}/${page.handle}`, + url: `${BASE_URL}/${page.handle}`, lastModified: page.updatedAt })) ); diff --git a/lib/constants.ts b/lib/constants.ts index 56bc6cd12d..45e2b0bb62 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -29,3 +29,10 @@ export const TAGS = { export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden'; export const DEFAULT_OPTION = 'Default Title'; export const SHOPIFY_GRAPHQL_API_ENDPOINT = '/api/2023-01/graphql.json'; + +export const BASE_URL = + process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' && process.env.TWITTER_SITE + ? process.env.TWITTER_SITE + : process.env.NEXT_PUBLIC_VERCEL_URL + ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` + : 'http://localhost:3000';