Skip to content

Commit

Permalink
fix: check for server-client cross env var configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
expries committed Oct 2, 2024
1 parent 89c64c0 commit dbd1391
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion app/config/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { z } = require('zod');
const { createEnvConfig, createZodSchemaFromEnvConfig } = require('./envConfig');
const { clientConfig } = require('./client');

if (typeof window !== 'undefined') {
throw new Error('The server config should not be imported on the frontend!');
Expand Down Expand Up @@ -151,12 +152,24 @@ const serverEnvConfig = createEnvConfig({
stages: runtimeStages,
defaultRule: z.string().optional(),
},
NEXT_PUBLIC_VAPID_PUBLIC_KEY: {
stage: runtimeStages,
defaultRule: z.string().optional(),
},

// Application Insights
APP_INSIGHTS_SERVICE_NAME: {
stages: runtimeStages,
defaultRule: z.string().optional(),
},
NEXT_PUBLIC_APP_INSIGHTS_CONNECTION_STRING: {
stages: runtimeStages,
defaultRule: z.string().optional(),
},
NEXT_PUBLIC_APP_INSIGHTS_INSTRUMENTATION_KEY: {
stages: runtimeStages,
defaultRule: z.string().optional(),
},

// Bundle analyzer
ANALYZE: {
Expand Down Expand Up @@ -198,11 +211,39 @@ const serverEnvConfig = createEnvConfig({
stages: runtimeStages,
errorMessage: 'At least one type of authentication has to be set',
},
{
variables: [
'VAPID_PRIVATE_KEY',
'VAPID_ADMIN_EMAIL',
'STRAPI_PUSH_NOTIFICATION_SECRET',
'NEXT_PUBLIC_VAPID_PUBLIC_KEY',
],
mode: 'none_or_all',
stages: runtimeStages,
errorMessage:
'Looks like the required environment variables for push-notifications are not set in the UI but in the server (or vice versa)',
},
{
variables: [
'APP_INSIGHTS_SERVICE_NAME',
'NEXT_PUBLIC_APP_INSIGHTS_CONNECTION_STRING',
'NEXT_PUBLIC_APP_INSIGHTS_INSTRUMENTATION_KEY',
],
mode: 'none_or_all',
stages: runtimeStages,
errorMessage:
'Looks like the required environment variables for ApplicationInsights are not set in the UI but in the server (or vice versa)',
},
],
});

const schema = createZodSchemaFromEnvConfig(serverEnvConfig);
const serverConfig = schema.safeParse(process.env);
const serverConfig = schema.safeParse({
...process.env,
NEXT_PUBLIC_VAPID_PUBLIC_KEY: clientConfig.NEXT_PUBLIC_VAPID_PUBLIC_KEY,
NEXT_PUBLIC_APP_INSIGHTS_CONNECTION_STRING: clientConfig.NEXT_PUBLIC_APP_INSIGHTS_CONNECTION_STRING,
NEXT_PUBLIC_APP_INSIGHTS_INSTRUMENTATION_KEY: clientConfig.NEXT_PUBLIC_APP_INSIGHTS_INSTRUMENTATION_KEY,
});

if (!serverConfig.success) {
console.error(serverConfig.error.issues);
Expand Down

0 comments on commit dbd1391

Please sign in to comment.