Skip to content

Commit 3266808

Browse files
authoredJul 8, 2024··
fix: only clear stale functions with build through CLI (#2536)
1 parent cfe1ac6 commit 3266808

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed
 

‎src/build/functions/edge.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ const buildHandlerDefinition = (
162162
}))
163163
}
164164

165-
export const createEdgeHandlers = async (ctx: PluginContext) => {
165+
export const clearStaleEdgeHandlers = async (ctx: PluginContext) => {
166166
await rm(ctx.edgeFunctionsDir, { recursive: true, force: true })
167+
}
167168

169+
export const createEdgeHandlers = async (ctx: PluginContext) => {
168170
const nextManifest = await ctx.getMiddlewareManifest()
169171
const nextDefinitions = [
170172
...Object.values(nextManifest.middleware),

‎src/build/functions/server.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,15 @@ const writeHandlerFile = async (ctx: PluginContext) => {
127127
await writeFile(join(ctx.serverHandlerRootDir, `${SERVER_HANDLER_NAME}.mjs`), handler)
128128
}
129129

130+
export const clearStaleServerHandlers = async (ctx: PluginContext) => {
131+
await rm(ctx.serverFunctionsDir, { recursive: true, force: true })
132+
}
133+
130134
/**
131135
* Create a Netlify function to run the Next.js server
132136
*/
133137
export const createServerHandler = async (ctx: PluginContext) => {
134138
await tracer.withActiveSpan('createServerHandler', async () => {
135-
await rm(ctx.serverFunctionsDir, { recursive: true, force: true })
136139
await mkdir(join(ctx.serverHandlerDir, '.netlify'), { recursive: true })
137140

138141
await copyNextServerCode(ctx)

‎src/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
publishStaticDir,
1414
unpublishStaticDir,
1515
} from './build/content/static.js'
16-
import { createEdgeHandlers } from './build/functions/edge.js'
17-
import { createServerHandler } from './build/functions/server.js'
16+
import { clearStaleEdgeHandlers, createEdgeHandlers } from './build/functions/edge.js'
17+
import { clearStaleServerHandlers, createServerHandler } from './build/functions/server.js'
1818
import { setImageConfig } from './build/image-cdn.js'
1919
import { PluginContext } from './build/plugin-context.js'
2020
import {
@@ -38,8 +38,15 @@ export const onPreBuild = async (options: NetlifyPluginOptions) => {
3838
await tracer.withActiveSpan('onPreBuild', async () => {
3939
// Enable Next.js standalone mode at build time
4040
process.env.NEXT_PRIVATE_STANDALONE = 'true'
41-
if (!options.constants.IS_LOCAL) {
42-
await restoreBuildCache(new PluginContext(options))
41+
const ctx = new PluginContext(options)
42+
if (options.constants.IS_LOCAL) {
43+
// Only clear directory if we are running locally as then we might have stale functions from previous
44+
// local builds. Directory clearing interferes with other integrations by deleting functions produced by them
45+
// so ideally this is completely avoided.
46+
await clearStaleServerHandlers(ctx)
47+
await clearStaleEdgeHandlers(ctx)
48+
} else {
49+
await restoreBuildCache(ctx)
4350
}
4451
})
4552
}

0 commit comments

Comments
 (0)
Please sign in to comment.