Skip to content

Commit d2d41fa

Browse files
committed
fix: make environment variables for draft mode available at edge
1 parent 8e4d7b6 commit d2d41fa

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/build/functions/edge.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi
119119

120120
const copyHandlerDependencies = async (
121121
ctx: PluginContext,
122-
{ name, files, wasm }: NextDefinition,
122+
{ name, env, files, wasm }: NextDefinition,
123123
) => {
124124
const srcDir = join(ctx.standaloneDir, ctx.nextDistDir)
125125
const destDir = join(ctx.edgeFunctionsDir, getHandlerName({ name }))
@@ -132,6 +132,11 @@ const copyHandlerDependencies = async (
132132

133133
const outputFile = join(destDir, `server/${name}.js`)
134134

135+
// Prepare environment variables for draft-mode (i.e. __NEXT_PREVIEW_MODE_ID, __NEXT_PREVIEW_MODE_SIGNING_KEY, __NEXT_PREVIEW_MODE_ENCRYPTION_KEY)
136+
for (const [key, value] of Object.entries(env)) {
137+
parts.push(`process.env.${key} = '${value}';`)
138+
}
139+
135140
if (wasm?.length) {
136141
for (const wasmChunk of wasm ?? []) {
137142
const data = await readFile(join(srcDir, wasmChunk.filePath))
@@ -161,16 +166,16 @@ const buildHandlerDefinition = (
161166
ctx: PluginContext,
162167
{ name, matchers, page }: NextDefinition,
163168
): Array<ManifestFunction> => {
164-
const fun = getHandlerName({ name })
165-
const funName = name.endsWith('middleware')
169+
const functionHandlerName = getHandlerName({ name })
170+
const functionName = name.endsWith('middleware')
166171
? 'Next.js Middleware Handler'
167172
: `Next.js Edge Handler: ${page}`
168173
const cache = name.endsWith('middleware') ? undefined : ('manual' as const)
169174
const generator = `${ctx.pluginName}@${ctx.pluginVersion}`
170175

171176
return augmentMatchers(matchers, ctx).map((matcher) => ({
172-
function: fun,
173-
name: funName,
177+
function: functionHandlerName,
178+
name: functionName,
174179
pattern: matcher.regexp,
175180
cache,
176181
generator,
@@ -183,10 +188,7 @@ export const clearStaleEdgeHandlers = async (ctx: PluginContext) => {
183188

184189
export const createEdgeHandlers = async (ctx: PluginContext) => {
185190
const nextManifest = await ctx.getMiddlewareManifest()
186-
const nextDefinitions = [
187-
...Object.values(nextManifest.middleware),
188-
// ...Object.values(nextManifest.functions)
189-
]
191+
const nextDefinitions = [...Object.values(nextManifest.middleware)]
190192
await Promise.all(nextDefinitions.map((def) => createEdgeHandler(ctx, def)))
191193

192194
const netlifyDefinitions = nextDefinitions.flatMap((def) => buildHandlerDefinition(ctx, def))

0 commit comments

Comments
 (0)