refactor baseUrl to consider vercel prod env and existing env var for prod URL (TWITTER_SITE)

This commit is contained in:
valiant1x 2024-04-17 10:22:33 -05:00
parent 610b0e8692
commit 77035c9362
4 changed files with 17 additions and 17 deletions

View File

@ -3,16 +3,14 @@ import { GeistSans } from 'geist/font';
import { ensureStartsWith } from 'lib/utils'; import { ensureStartsWith } from 'lib/utils';
import { ReactNode, Suspense } from 'react'; import { ReactNode, Suspense } from 'react';
import './globals.css'; import './globals.css';
import { BASE_URL } from 'lib/constants';
const { TWITTER_CREATOR, TWITTER_SITE, SITE_NAME } = process.env; 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 twitterCreator = TWITTER_CREATOR ? ensureStartsWith(TWITTER_CREATOR, '@') : undefined;
const twitterSite = TWITTER_SITE ? ensureStartsWith(TWITTER_SITE, 'https://') : undefined; const twitterSite = TWITTER_SITE ? ensureStartsWith(TWITTER_SITE, 'https://') : undefined;
export const metadata = { export const metadata = {
metadataBase: new URL(baseUrl), metadataBase: new URL(BASE_URL),
title: { title: {
default: SITE_NAME!, default: SITE_NAME!,
template: `%s | ${SITE_NAME}` template: `%s | ${SITE_NAME}`

View File

@ -1,6 +1,4 @@
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL import { BASE_URL } from "lib/constants";
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: 'http://localhost:3000';
export default function robots() { export default function robots() {
return { return {
@ -9,7 +7,7 @@ export default function robots() {
userAgent: '*' userAgent: '*'
} }
], ],
sitemap: `${baseUrl}/sitemap.xml`, sitemap: `${BASE_URL}/sitemap.xml`,
host: baseUrl host: BASE_URL
}; };
} }

View File

@ -1,3 +1,4 @@
import { BASE_URL } from 'lib/constants';
import { getCollections, getPages, getProducts } from 'lib/shopify'; import { getCollections, getPages, getProducts } from 'lib/shopify';
import { validateEnvironmentVariables } from 'lib/utils'; import { validateEnvironmentVariables } from 'lib/utils';
import { MetadataRoute } from 'next'; import { MetadataRoute } from 'next';
@ -7,35 +8,31 @@ type Route = {
lastModified: string; 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<MetadataRoute.Sitemap> { export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
validateEnvironmentVariables(); validateEnvironmentVariables();
const routesMap = [''].map((route) => ({ const routesMap = [''].map((route) => ({
url: `${baseUrl}${route}`, url: `${BASE_URL}${route}`,
lastModified: new Date().toISOString() lastModified: new Date().toISOString()
})); }));
const collectionsPromise = getCollections().then((collections) => const collectionsPromise = getCollections().then((collections) =>
collections.map((collection) => ({ collections.map((collection) => ({
url: `${baseUrl}${collection.path}`, url: `${BASE_URL}${collection.path}`,
lastModified: collection.updatedAt lastModified: collection.updatedAt
})) }))
); );
const productsPromise = getProducts({}).then((products) => const productsPromise = getProducts({}).then((products) =>
products.map((product) => ({ products.map((product) => ({
url: `${baseUrl}/product/${product.handle}`, url: `${BASE_URL}/product/${product.handle}`,
lastModified: product.updatedAt lastModified: product.updatedAt
})) }))
); );
const pagesPromise = getPages().then((pages) => const pagesPromise = getPages().then((pages) =>
pages.map((page) => ({ pages.map((page) => ({
url: `${baseUrl}/${page.handle}`, url: `${BASE_URL}/${page.handle}`,
lastModified: page.updatedAt lastModified: page.updatedAt
})) }))
); );

View File

@ -29,3 +29,10 @@ export const TAGS = {
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden'; export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';
export const DEFAULT_OPTION = 'Default Title'; export const DEFAULT_OPTION = 'Default Title';
export const SHOPIFY_GRAPHQL_API_ENDPOINT = '/api/2023-01/graphql.json'; 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';