turbopack: ensure ENV values are available in middleware (#47767)

Turbopack starts up the router process with all ENV values, but the edge
function definition didn't list any `env` keys for the function
invocation. So, middleware couldn't access any ENV values.

Turbopack doesn't currently have a way to determine what ENV keys are
actually used by the source program, so I'm just passing everything
defined. I'm not sure if that's an issue during dev (I could see it
being one for the build process, but that doesn't matter for this case).

Fixes #47766
Fixes WEB-831
Fixes WEB-834
This commit is contained in:
Justin Ridgewell 2023-03-31 20:22:27 -04:00 committed by GitHub
parent 2820f07875
commit 82787dba9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 1 deletions

View file

@ -0,0 +1,10 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(_request: NextRequest) {
return NextResponse.json(process.env)
}
export const config = {
matcher: '/body',
}

View file

@ -0,0 +1,2 @@
/** @type {import('next').NextConfig} */
module.exports = {}

View file

@ -0,0 +1,18 @@
import { useEffect } from 'react'
export default function Foo() {
useEffect(() => {
// Only run on client
import('@turbo/pack-test-harness').then(runTests)
})
return 'index'
}
function runTests() {
it('should include custom env fields in middleware process', async () => {
const res = await fetch('/body')
const env = await res.json()
expect(env).toHaveProperty('CUSTOM_ENV', 'foobar')
})
}

View file

@ -159,7 +159,7 @@ export async function makeResolver(
return {
name: 'middleware',
paths: middleware.files.map((file) => join(process.cwd(), file)),
env: [],
env: Object.keys(process.env),
wasm: [],
assets: [],
}