From 2e61590b01d6df9321e6214da16004ac0bfa2712 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/logging/logger.ts | 8 +------- .../core/networkInformation/networkInformation.ts | 1 - src/pages/api/status.ts | 1 + 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/next.config.js b/next.config.js index cb182962..2265b621 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/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/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);