Skip to content

Commit

Permalink
Fix legacy env case with experimental tracing (#70411)
Browse files Browse the repository at this point in the history
This ensures `env` keys in `next.config` are properly exposed during
page collecting step during build since the inlining step comes after.

x-ref: #70380
x-ref:
https://vercel.com/vercel/vercel-site/9WqUX9vu15oVEy1acDqoXddcH27K?filter=errors
  • Loading branch information
ijjk committed Sep 24, 2024
1 parent 538b1c6 commit 900a807
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,15 @@ export default async function build(
await fs.mkdir(shuttleDir, {
recursive: true,
})

// since inlining comes after static generation we need
// to ensure this value is assigned to process env so it
// can still be accessed
for (const key in config.env) {
if (!process.env[key]) {
process.env[key] = config.env[key]
}
}
}

const customRoutes: CustomRoutes = await nextBuildSpan
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/app/legacy-env/page.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export default function Page() {
if (!process.env.LEGACY_ENV_KEY) {
throw new Error('missing env key LEGACY_ENV_KEY!!')
}
return <p id="legacy-env">{process.env.LEGACY_ENV_KEY}</p>
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/pages/ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default function Page(props) {
}

export function getStaticProps() {
if (!process.env.LEGACY_ENV_KEY) {
throw new Error('missing env key LEGACY_ENV_KEY!!')
}
return {
props: {},
}
Expand Down

0 comments on commit 900a807

Please sign in to comment.