Unverified Commit 10b93557 authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

turbopack: pass ENV vars to route handlers (#48048)

Same as https://github.com/vercel/next.js/pull/47767, we need to pass
the full list of `process.env` keys to the edge invocation so that the
ENV vars are available to the route handlers.

Fixes https://github.com/vercel/turbo/issues/4489
Fixes https://github.com/vercel/next.js/issues/48036
Fixes WEB-869
Fixes NEXT-961
fix #48036
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 8d3e42e5
// IPC need to be the first import to allow it to catch errors happening during // IPC need to be the first import to allow it to catch errors happening during
// the other imports // the other imports
import startHandler from "@vercel/turbopack-next/internal/api-server-handler"; import startHandler from '@vercel/turbopack-next/internal/api-server-handler'
import { runEdgeFunction } from "@vercel/turbopack-next/internal/edge"; import { runEdgeFunction } from '@vercel/turbopack-next/internal/edge'
import { join } from "path"; import { join } from 'path'
import "next/dist/server/node-polyfill-fetch.js"; import 'next/dist/server/node-polyfill-fetch.js'
import chunkGroup from "ROUTE_CHUNK_GROUP"; import chunkGroup from 'ROUTE_CHUNK_GROUP'
import { import {
NodeNextRequest, NodeNextRequest,
NodeNextResponse, NodeNextResponse,
} from "next/dist/server/base-http/node"; } from 'next/dist/server/base-http/node'
startHandler(async ({ request, response, query, params, path }) => { startHandler(async ({ request, response, query, params, path }) => {
const edgeInfo = { const edgeInfo = {
name: "edge", name: 'edge',
paths: chunkGroup.map((chunk) => paths: chunkGroup.map((chunk) =>
join(process.cwd(), ".next/server/app", chunk) join(process.cwd(), '.next/server/app', chunk)
), ),
wasm: [], wasm: [],
env: [], env: Object.keys(process.env),
assets: [], assets: [],
}; }
await runEdgeFunction({ await runEdgeFunction({
edgeInfo, edgeInfo,
outputDir: "app", outputDir: 'app',
req: new NodeNextRequest(request), req: new NodeNextRequest(request),
res: new NodeNextResponse(response), res: new NodeNextResponse(response),
query, query,
params, params,
path, path,
onWarning(warning) { onWarning(warning) {
console.warn(warning); console.warn(warning)
}, },
}); })
}); })
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export const helloHandler = async (_req: NextRequest): Promise<Response> => {
return NextResponse.json(process.env)
}
export const GET = helloHandler
export default function RootLayout({ children }: { children: any }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Test from './test'
export default function Page() {
return (
<div>
<Test />
</div>
)
}
'use client'
import { useEffect } from 'react'
export default function Test() {
useEffect(() => {
import('@turbo/pack-test-harness').then(() => {
it('includes ENV vars in route edge runner', async () => {
const res = await fetch('/route')
const json = await res.json()
expect(json).toMatchObject({
FOOBAR: 'foobar',
})
}, 20000)
})
return () => {}
}, [])
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment