Introduce APP_ENV variable to frontend#98
Merged
Merged
Conversation
|
🚅 Deployed to the NoteBlockWorld-pr-98 environment in Note Block World
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Some website features, such as crawler indexing (
robots.txt), ads, and analytics should only ever be enabled in production deployments. So far, we had been usingNODE_ENVto signal whether or not we were running a production deployment. As mentioned in the Node.js documentation, this usage is considered an antipattern. The only acceptable value for theNODE_ENVvariable isproductionwhich by convention indicates we're running with production-mode optimizations (or the absence of it, which signals otherwise) for some libraries.This means that
NODE_ENV=productionis the appropriate mode for us to run our development/staging environments on, which we already do. However, this means that our previous checks/assumptions for whether we're running in production were wrong, because our development environment also runs withNODE_ENV=production. In turn,robots.txtwas not avoiding crawling, ad code was being loaded etc. in our development environment.This PR introduces the
APP_ENVenvironment variable on the frontend, which can assume valueslocal,development,stagingandproduction.NODE_ENV is always supposed to beproduction` in non-development environments.Additions
APP_ENVutilityNew shared helper at
apps/frontend/src/lib/appEnv.ts:local,development,staging,productionAPP_ENV(server/build) orNEXT_PUBLIC_APP_ENV(client)localif unset or invalidgetAppEnv(),isProductionAppEnv(),isLocalAppEnv()next.config.mjsAPP_ENVis forwarded to the client at build time viaNEXT_PUBLIC_APP_ENV, so client components can use the same value without duplicating env vars.Updated call sites
All behavioral
NODE_ENVchecks in the frontend now useAPP_ENV:robots.tsAPP_ENV=productionlayout.tsxGoogleAdSense.tsxuseAdSenseClient.tsxlogin.util.tsisLocalAppEnv()instead ofNODE_ENV === 'development'Configuration
Set
APP_ENVper deployment:On staging, set
APP_ENV=staging(ordevelopment) even ifNODE_ENV=productionfor the Next.js build. That keeps build optimizations while blocking crawlers, ads, and analytics.apps/frontend/.env.local.examplealready includesAPP_ENV=local.This matches the Node.js guidance: keep
NODE_ENV=productionfor builds, and useAPP_ENVfor deployment-specific behavior.