From d7dbfddcac744737be87f9065ae817f3a8efab16 Mon Sep 17 00:00:00 2001 From: Vadorequest Date: Thu, 10 Jun 2021 16:35:28 +0200 Subject: [PATCH] Fix usage of IS_SERVER_INITIAL_BUILD in logging + improve doc (#368) --- next.config.js | 13 ++++++++++++- src/modules/core/airtable/fetchAirtableDataset.ts | 6 ++++++ src/modules/core/logging/logger.ts | 8 +------- .../core/networkInformation/networkInformation.ts | 1 - src/modules/core/testing/tests/env.test.ts | 1 + src/pages/api/status.ts | 1 + 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/next.config.js b/next.config.js index cf8f9871..f7055202 100644 --- a/next.config.js +++ b/next.config.js @@ -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'; } diff --git a/src/modules/core/airtable/fetchAirtableDataset.ts b/src/modules/core/airtable/fetchAirtableDataset.ts index fba43f9f..86b3e7e9 100644 --- a/src/modules/core/airtable/fetchAirtableDataset.ts +++ b/src/modules/core/airtable/fetchAirtableDataset.ts @@ -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'; @@ -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. diff --git a/src/modules/core/logging/logger.ts b/src/modules/core/logging/logger.ts index 61239197..a072d95c 100644 --- a/src/modules/core/logging/logger.ts +++ b/src/modules/core/logging/logger.ts @@ -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()); }, }); diff --git a/src/modules/core/networkInformation/networkInformation.ts b/src/modules/core/networkInformation/networkInformation.ts index c6a13dfb..0fd307d3 100644 --- a/src/modules/core/networkInformation/networkInformation.ts +++ b/src/modules/core/networkInformation/networkInformation.ts @@ -71,7 +71,6 @@ export const getClientNetworkConnectionType = (): ClientNetworkConnectionType => } else { return 'not-applicable'; } - console.log('networkInformation', networkInformation); return networkInformation?.type; }; diff --git a/src/modules/core/testing/tests/env.test.ts b/src/modules/core/testing/tests/env.test.ts index 17e7c4ef..f2aa3297 100644 --- a/src/modules/core/testing/tests/env.test.ts +++ b/src/modules/core/testing/tests/env.test.ts @@ -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') { diff --git a/src/pages/api/status.ts b/src/pages/api/status.ts index 7accd3dc..39b01005 100644 --- a/src/pages/api/status.ts +++ b/src/pages/api/status.ts @@ -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);