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

Allow dependencies to use environment variables in middlewares #33141

Merged
merged 4 commits into from
Jan 10, 2022
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
1 change: 1 addition & 0 deletions packages/next/build/polyfills/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = global.process || require('../../compiled/process')
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ export default async function getBaseWebpackConfig(
os: require.resolve('next/dist/compiled/os-browserify'),
path: require.resolve('next/dist/compiled/path-browserify'),
punycode: require.resolve('next/dist/compiled/punycode'),
process: require.resolve('next/dist/compiled/process'),
process: require.resolve('./polyfills/process'),
// Handled in separate alias
querystring: require.resolve('next/dist/compiled/querystring-es3'),
// TODO: investigate ncc'ing stream-browserify
Expand Down
4 changes: 1 addition & 3 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ export default class MiddlewarePlugin {
.tap(PLUGIN_NAME, ignore)

const memberChainHandler = (_expr: any, members: string[]) => {
if (!isMiddlewareModule()) return

if (members.length >= 2 && members[0] === 'env') {
const envName = members[1]
const { buildInfo } = parser.state.module
Expand All @@ -305,7 +303,7 @@ export default class MiddlewarePlugin {
}

buildInfo.nextUsedEnvVars.add(envName)
return true
if (isMiddlewareModule()) return true
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import { readJson } from 'fs-extra'
import path from 'path'

describe('dependencies can use env vars in middlewares', () => {
let next: NextInstance

beforeAll(() => {
process.env.MY_CUSTOM_PACKAGE_ENV_VAR = 'my-custom-package-env-var'
process.env.ENV_VAR_USED_IN_MIDDLEWARE = 'env-var-used-in-middleware'
})

beforeAll(async () => {
next = await createNext({
files: {
// A 3rd party dependency
'node_modules/my-custom-package/package.json': JSON.stringify({
name: 'my-custom-package',
version: '1.0.0',
browser: 'index.js',
}),
'node_modules/my-custom-package/index.js': `
module.exports = () => process.env.MY_CUSTOM_PACKAGE_ENV_VAR;
`,

// The actual middleware code
'pages/api/_middleware.js': `
import customPackage from 'my-custom-package';
export default function middleware(_req) {
return new Response(JSON.stringify({
string: "a constant string",
hello: process.env.ENV_VAR_USED_IN_MIDDLEWARE,
customPackage: customPackage(),
}), {
headers: {
'Content-Type': 'application/json'
}
})
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it('parses the env vars correctly', async () => {
const testDir = next.testDir
const manifestPath = path.join(
testDir,
'.next/server/middleware-manifest.json'
)
const manifest = await readJson(manifestPath)
const envVars = manifest?.middleware?.['/api']?.env

expect(envVars).toHaveLength(2)
expect(envVars).toContain('ENV_VAR_USED_IN_MIDDLEWARE')
expect(envVars).toContain('MY_CUSTOM_PACKAGE_ENV_VAR')
})

it('uses the environment variables', async () => {
const html = await renderViaHTTP(next.url, '/api')
expect(html).toContain(
JSON.stringify({
string: 'a constant string',
hello: 'env-var-used-in-middleware',
customPackage: 'my-custom-package-env-var',
})
)
})
})