Skip to content

Commit 5cb1245

Browse files
manovotnyleerob
andauthored
Adds environment variable validation (vercel#1198)
* Adds environment variable validation * Adds bracket checking in SHOPIFY_STORE_DOMAIN * Prettier * Adds link --------- Co-authored-by: Lee Robinson <[email protected]>
1 parent d9f875b commit 5cb1245

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

app/sitemap.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getCollections, getPages, getProducts } from 'lib/shopify';
2+
import { validateEnvironmentVariables } from 'lib/utils';
23
import { MetadataRoute } from 'next';
34

45
type Route = {
@@ -11,6 +12,8 @@ const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
1112
: 'http://localhost:3000';
1213

1314
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
15+
validateEnvironmentVariables();
16+
1417
const routesMap = [''].map((route) => ({
1518
url: `${baseUrl}${route}`,
1619
lastModified: new Date().toISOString()

lib/utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,31 @@ export const createUrl = (pathname: string, params: URLSearchParams | ReadonlyUR
99

1010
export const ensureStartsWith = (stringToCheck: string, startsWith: string) =>
1111
stringToCheck.startsWith(startsWith) ? stringToCheck : `${startsWith}${stringToCheck}`;
12+
13+
export const validateEnvironmentVariables = () => {
14+
const requiredEnvironmentVariables = ['SHOPIFY_STORE_DOMAIN', 'SHOPIFY_STOREFRONT_ACCESS_TOKEN'];
15+
const missingEnvironmentVariables = [] as string[];
16+
17+
requiredEnvironmentVariables.forEach((envVar) => {
18+
if (!process.env[envVar]) {
19+
missingEnvironmentVariables.push(envVar);
20+
}
21+
});
22+
23+
if (missingEnvironmentVariables.length) {
24+
throw new Error(
25+
`The following environment variables are missing. Your site will not work without them. Read more: https://vercel.com/docs/integrations/shopify#configure-environment-variables\n\n${missingEnvironmentVariables.join(
26+
'\n'
27+
)}\n`
28+
);
29+
}
30+
31+
if (
32+
process.env.SHOPIFY_STORE_DOMAIN?.includes('[') ||
33+
process.env.SHOPIFY_STORE_DOMAIN?.includes(']')
34+
) {
35+
throw new Error(
36+
'Your `SHOPIFY_STORE_DOMAIN` environment variable includes brackets (ie. `[` and / or `]`). Your site will not work with them there. Please remove them.'
37+
);
38+
}
39+
};

0 commit comments

Comments
 (0)