Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor baseUrl to fix exposing NEXT_PUBLIC_VERCEL_URL #1326

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
6 changes: 2 additions & 4 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import { cookies } from 'next/headers';
import { ReactNode } from 'react';
import { Toaster } from 'sonner';
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}`
Expand Down
8 changes: 3 additions & 5 deletions app/robots.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,7 +7,7 @@ export default function robots() {
userAgent: '*'
}
],
sitemap: `${baseUrl}/sitemap.xml`,
host: baseUrl
sitemap: `${BASE_URL}/sitemap.xml`,
host: BASE_URL
};
}
13 changes: 5 additions & 8 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -7,37 +8,33 @@ type Route = {
lastModified: string;
};

const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: 'http://localhost:3000';

export const dynamic = 'force-dynamic';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
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
}))
);
Expand Down
7 changes: 7 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to use VERCEL_PROJECT_PRODUCTION_URL here now.

https://vercel.com/docs/projects/environment-variables/system-environment-variables#system-environment-variables

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump on this! Are you still interested in taking this PR?

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';