Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix legacy env config with experimental tracing #70380

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions packages/next/src/build/flying-shuttle/inline-static-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@ import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import globOriginal from 'next/dist/compiled/glob'
import { getNextPublicEnvironmentVariables } from '../webpack/plugins/define-env-plugin'
import {
getNextConfigEnv,
getNextPublicEnvironmentVariables,
} from '../webpack/plugins/define-env-plugin'
import { Sema } from 'next/dist/compiled/async-sema'
import type { NextConfigComplete } from '../../server/config-shared'

const glob = promisify(globOriginal)

export async function inlineStaticEnv({ distDir }: { distDir: string }) {
const staticEnv = getNextPublicEnvironmentVariables()
export async function inlineStaticEnv({
distDir,
config,
}: {
distDir: string
config: NextConfigComplete
}) {
const nextConfigEnv = getNextConfigEnv(config)

const staticEnv = {
...getNextPublicEnvironmentVariables(),
...nextConfigEnv,
}

const serverDir = path.join(distDir, 'server')
const serverChunks = await glob('**/*.js', {
Expand All @@ -20,6 +35,14 @@ export async function inlineStaticEnv({ distDir }: { distDir: string }) {
})

const inlineSema = new Sema(8)
const nextConfigEnvKeys = Object.keys(nextConfigEnv).map((item) =>
item.split('process.env.').pop()
)

const builtRegEx = new RegExp(
`[\\w]{1,}\\.env\\.(?:NEXT_PUBLIC_[\\w]{1,}${nextConfigEnvKeys.length ? '|' + nextConfigEnvKeys.join('|') : ''})`,
'g'
)

for (const [parentDir, files] of [
[serverDir, serverChunks],
Expand All @@ -33,7 +56,7 @@ export async function inlineStaticEnv({ distDir }: { distDir: string }) {

await fs.promises.writeFile(
filepath,
content.replace(/[\w]{1,}\.env\.NEXT_PUBLIC_[\w]{1,}/g, (match) => {
content.replace(builtRegEx, (match) => {
let normalizedMatch = `process.env.${match.split('.').pop()}`

if (staticEnv[normalizedMatch]) {
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/build/flying-shuttle/store-shuttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function generateShuttleManifest(config: NextConfigComplete) {
gitSha,
nextVersion,
config: {
env: config.env,
i18n: config.i18n,
basePath: config.basePath,
sassOptions: config.sassOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2670,7 +2670,7 @@ export default async function build(
await nextBuildSpan
.traceChild('inline-static-env')
.traceAsyncFn(async () => {
await inlineStaticEnv({ distDir })
await inlineStaticEnv({ config, distDir })
})
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/next/src/build/webpack/plugins/define-env-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function getNextPublicEnvironmentVariables(): DefineEnv {
/**
* Collects the `env` config value from the Next.js config.
*/
function getNextConfigEnv(config: NextConfigComplete): DefineEnv {
export function getNextConfigEnv(config: NextConfigComplete): DefineEnv {
// Refactored code below to use for-of
const defineEnv: DefineEnv = {}
const env = config.env
Expand Down Expand Up @@ -286,9 +286,10 @@ export function getDefineEnv({
// with flying shuttle enabled so we can update them
// without invalidating entries
for (const key in nextPublicEnv) {
if (key in nextConfigEnv) {
continue
}
serializedDefineEnv[key] = key
}

for (const key in nextConfigEnv) {
serializedDefineEnv[key] = key
}
}
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
@@ -0,0 +1,3 @@
export default function Page() {
return <p id="legacy-env">{process.env.LEGACY_ENV_KEY}</p>
}
28 changes: 28 additions & 0 deletions test/e2e/app-dir/app/flying-shuttle.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import cheerio from 'cheerio'
import { version as nextVersion } from 'next/package.json'
import type { Route } from 'playwright'
import { retry } from 'next-test-utils'
Expand Down Expand Up @@ -423,5 +424,32 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
await next.patchFile(dataPath, originalDataContent)
}
})

it('should not invalidate on legacy next env but inline properly', async () => {
await next.stop()

const dataPath = 'next.config.js'
const originalDataContent = await next.readFile(dataPath)

try {
await next.patchFile(
dataPath,
originalDataContent.replace(
`LEGACY_ENV_KEY: '1'`,
`LEGACY_ENV_KEY: '2'`
)
)
await nextStart()

const res = await next.fetch('/legacy-env')
const html = await res.text()
const $ = cheerio.load(html)

expect(res.status).toBe(200)
expect($('#legacy-env').text()).toBe('2')
} finally {
await next.patchFile(dataPath, originalDataContent)
}
})
}
)
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* @type import('next').NextConfig
*/
module.exports = {
env: {
LEGACY_ENV_KEY: '1',
},
experimental: {
clientRouterFilterRedirects: true,
parallelServerCompiles: true,
Expand Down
Loading