Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(nextjs): Make build-phase check more robust (#5857)
This makes a few changes to the `isBuild` utility function in the nextjs SDK in order to make its operation less brittle. Currently, it relies on being called at the top level of `index.server.ts`, which ensures that it runs early enough in the build process to be executed in the main thread, before operation has been split out into child processes. (This is important because child processes don't conveniently have the word 'build' in their invocations the way the main thread (invoked by `next build`) does, removing one of `isBuild`'s clues about what phase it's in.) It then sets an environment variable as a clue to future calls to itself, so that, child processes or not, it knows the correct phase. This means, however, that if it stops being used at `index.server.ts`'s top level, and doesn't get called until after the process split, it won't have any way to know whether it's in the build phase or not. This makes two changes to guard against those possibilities: - To ensure that it's always called as early as possible in the build, it is now run independently of any use of its return value, instead storing that value in a constant, which is then used anywhere it's needed. - To provide a backup indicator of the current phase, it now also checks the next-provided `NEXT_PHASE` environment variable. (The reason it doesn't simply rely on this variable to begin with is that it's only set partway through the build process.) As a result of these changes, two other changes are included here: - The export of `isBuild` is deprecated in favor of the computed constant. (Since they're equivalent, no reason to keep exporting them both.) - The local constant `isVercel` has been renamed `IS_VERCEL`, to match `IS_BUILD`.
- Loading branch information