Skip to content

Commit 34613ff

Browse files
committed
Fail more obviously if env vars are missing
1 parent aa25a5c commit 34613ff

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type AppConfig = {
1+
export type AppConfig = {
22
CONFIDENTIAL_ASSET_MODULE_ADDR: string;
33
/**
44
* This is the asset the user deals with in the app. For the demo they only use a
@@ -58,3 +58,11 @@ export const appConfig: AppConfig = {
5858

5959
FORCE_MAINTENANCE_PAGE: process.env.FORCE_MAINTENANCE_PAGE === 'true',
6060
};
61+
62+
// Iterate through the config and ensure nothing is undefined. This check runs client
63+
// side, so we only check NEXT_PUBLIC_ variables. We check everything in middleware.ts.
64+
for (const key in appConfig) {
65+
if (key.startsWith('NEXT_PUBLIC_') && appConfig[key as keyof AppConfig] === undefined) {
66+
throw new Error(`Required environment variable ${key} is not set.`);
67+
}
68+
}

src/middleware.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { createMiddleware, type MiddlewareFunctionProps } from '@rescale/nemo';
22
import { NextResponse } from 'next/server';
33

4-
import { appConfig } from './config';
4+
import { AppConfig, appConfig } from './config';
5+
6+
// Iterate through the config and ensure nothing is undefined.
7+
for (const key in appConfig) {
8+
if (appConfig[key as keyof AppConfig] === undefined) {
9+
throw new Error(`Required environment variable ${key} is not set.`);
10+
}
11+
}
512

613
async function shouldShowMaintenancePage() {
714
if (appConfig.FORCE_MAINTENANCE_PAGE) {

0 commit comments

Comments
 (0)