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 committed Jun 10, 2021
1 parent ec618be commit 2e61590
Show file tree
Hide file tree
Showing 4 changed files with 14 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
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/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 2e61590

Please sign in to comment.