Skip to content

Commit

Permalink
Fix usage of IS_SERVER_INITIAL_BUILD in logging + improve doc (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadorequest authored Jun 10, 2021
1 parent b0dd34f commit d7dbfdd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
13 changes: 12 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,18 @@ module.exports = withNextPluginPreval(withBundleAnalyzer(withSourceMaps({
defaultLoaders,
}) => {
if (isServer) {
// IS_SERVER_INITIAL_BUILD is meant to be defined only at build time and not at run time, and therefore must not be "made public"
/**
* This special server-only environment variable isn't string-replaced by webpack during bundling (it isn't added to the DefinePlugin definitions).
*
* Therefore, it's:
* - Always '1' on the server, during development
* - Always '1' on the server, during the Next.js build step
* - Always undefined on the browser
* - Always undefined in API endpoints
* - Always undefined during static pages re-generations (ISG) and server-side pages
*
* It can be useful when performing processing that should only happen during the initial build, or not during the initial build.
*/
process.env.IS_SERVER_INITIAL_BUILD = '1';
}

Expand Down
6 changes: 6 additions & 0 deletions src/modules/core/airtable/fetchAirtableDataset.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createLogger } from '@/modules/core/logging/logger';
import map from 'lodash.map';
import size from 'lodash.size';
import hybridCache from '../vercelCache/hybridCache';
Expand All @@ -9,6 +10,11 @@ import { GenericAirtableRecordsListApiResponse } from './types/GenericAirtableRe
import { RawAirtableRecordsSet } from './types/RawAirtableRecordsSet';
import { TableSchema } from './types/TableSchema';

const fileLabel = 'modules/core/airtable/fetchAirtableDataset.ts';
const logger = createLogger({
fileLabel,
});

/**
* When running on Vercel, wait some time after each API request to avoid running the next API request too fast
* If we don't do that, we might reach their API rate limit (5 requests per 1 second) and get blocked.
Expand Down
8 changes: 1 addition & 7 deletions src/modules/core/logging/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ export const createLogger = ({ fileLabel }: { fileLabel: string }): SimpleLogger
return createSimpleLogger({
prefix: fileLabel,
shouldPrint: (mode) => {
// When bundling with Webpack, only print errors/warnings to avoid printing too much noise on Vercel
// IS_SERVER_INITIAL_BUILD is always "1" during development, and is being ignored
if (process.env.NEXT_PUBLIC_APP_STAGE !== 'development' && process.env.IS_SERVER_INITIAL_BUILD === '1') {
return mode === 'error' || mode === 'warn' || mode === 'debug';
}

// Otherwise, only hide browser errors in production
// Only hide browser errors in production
return !(process.env.NEXT_PUBLIC_APP_STAGE === 'production' && isBrowser());
},
});
Expand Down
1 change: 0 additions & 1 deletion src/modules/core/networkInformation/networkInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export const getClientNetworkConnectionType = (): ClientNetworkConnectionType =>
} else {
return 'not-applicable';
}
console.log('networkInformation', networkInformation);

return networkInformation?.type;
};
1 change: 1 addition & 0 deletions src/modules/core/testing/tests/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe(`utils/env/env.ts`, () => {
expect(process.env.NEXT_PUBLIC_APP_BUILD_TIMESTAMP, 'NEXT_PUBLIC_APP_BUILD_TIMESTAMP should not be defined when building a non-production release').not.toBeDefined();
}
});

test(`IS_SERVER_INITIAL_BUILD`, async () => {
// This ENV var is injected by Webpack and not available when running the tests suite when building a local build (because webpack hasn't been executed yet)
if (process.env.NODE_ENV === 'production') {
Expand Down
1 change: 1 addition & 0 deletions src/pages/api/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const status = async (req: NextApiRequest, res: NextApiResponse): Promise
GIT_COMMIT_REF: process.env.GIT_COMMIT_REF,
GIT_COMMIT_TAGS: process.env.GIT_COMMIT_TAGS,
NEXT_PUBLIC_APP_BASE_URL: process.env.NEXT_PUBLIC_APP_BASE_URL,
IS_SERVER_INITIAL_BUILD: process.env.IS_SERVER_INITIAL_BUILD, // Shouldn't be displayed, because should always be undefined in APIs
});
} catch (e) {
Sentry.captureException(e);
Expand Down

0 comments on commit d7dbfdd

Please sign in to comment.