Skip to content

Commit c44f2b6

Browse files
fix: watch edge function dependencies (#4576)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 7c5f0e4 commit c44f2b6

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/lib/edge-functions/proxy.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ const handleProxyRequest = (req, proxyReq) => {
4242
})
4343
}
4444

45-
const initializeProxy = async ({ config, configPath, geolocationMode, getUpdatedConfig, offline, settings, state }) => {
45+
const initializeProxy = async ({
46+
config,
47+
configPath,
48+
geolocationMode,
49+
getUpdatedConfig,
50+
offline,
51+
projectDir,
52+
settings,
53+
state,
54+
}) => {
4655
const { functions: internalFunctions, importMap, path: internalFunctionsPath } = await getInternalFunctions()
4756
const { port: mainPort } = settings
4857
const userFunctionsPath = config.build.edge_functions
@@ -60,6 +69,7 @@ const initializeProxy = async ({ config, configPath, geolocationMode, getUpdated
6069
importMaps: [importMap].filter(Boolean),
6170
internalFunctions,
6271
port: isolatePort,
72+
projectDir,
6373
})
6474
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctions.length !== 0
6575

@@ -125,6 +135,7 @@ const prepareServer = async ({
125135
importMaps,
126136
internalFunctions,
127137
port,
138+
projectDir,
128139
}) => {
129140
const bundler = await import('@netlify/edge-bundler')
130141
const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH])
@@ -148,6 +159,7 @@ const prepareServer = async ({
148159
directories,
149160
getUpdatedConfig,
150161
internalFunctions,
162+
projectDir,
151163
runIsolate,
152164
})
153165

src/lib/edge-functions/registry.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// @ts-check
2+
const { fileURLToPath } = require('url')
3+
24
const { NETLIFYDEVERR, NETLIFYDEVLOG, chalk, log, warn, watchDebounced } = require('../../utils/command-helpers')
35

46
/**
@@ -24,9 +26,19 @@ class EdgeFunctionsRegistry {
2426
* @param {string[]} opts.directories
2527
* @param {() => Promise<object>} opts.getUpdatedConfig
2628
* @param {EdgeFunction[]} opts.internalFunctions
29+
* @param {string} opts.projectDir
2730
* @param {(functions: EdgeFunction[]) => Promise<object>} opts.runIsolate
2831
*/
29-
constructor({ bundler, config, configPath, directories, getUpdatedConfig, internalFunctions, runIsolate }) {
32+
constructor({
33+
bundler,
34+
config,
35+
configPath,
36+
directories,
37+
getUpdatedConfig,
38+
internalFunctions,
39+
projectDir,
40+
runIsolate,
41+
}) {
3042
/**
3143
* @type {import('@netlify/edge-bundler')}
3244
*/
@@ -87,7 +99,7 @@ class EdgeFunctionsRegistry {
8799
*/
88100
this.initialScan = this.scan(directories)
89101

90-
this.setupWatchers()
102+
this.setupWatchers({ projectDir })
91103
}
92104

93105
/**
@@ -283,7 +295,12 @@ class EdgeFunctionsRegistry {
283295
const dependencyPaths = new Map()
284296

285297
graph.modules.forEach(({ dependencies = [], specifier }) => {
286-
const functionMatch = functionPaths.get(specifier)
298+
if (!specifier.startsWith('file://')) {
299+
return
300+
}
301+
302+
const path = fileURLToPath(specifier)
303+
const functionMatch = functionPaths.get(path)
287304

288305
if (!functionMatch) {
289306
return
@@ -301,9 +318,10 @@ class EdgeFunctionsRegistry {
301318
}
302319

303320
const { specifier: dependencyURL } = dependency.code
304-
const functions = dependencyPaths.get(dependencyURL) || []
321+
const dependencyPath = fileURLToPath(dependencyURL)
322+
const functions = dependencyPaths.get(dependencyPath) || []
305323

306-
dependencyPaths.set(dependencyURL, [...functions, functionMatch])
324+
dependencyPaths.set(dependencyPath, [...functions, functionMatch])
307325
})
308326
})
309327

@@ -323,7 +341,7 @@ class EdgeFunctionsRegistry {
323341
return functions
324342
}
325343

326-
async setupWatchers() {
344+
async setupWatchers({ projectDir }) {
327345
// Creating a watcher for the config file. When it changes, we update the
328346
// declarations and see if we need to register or unregister any functions.
329347
this.configWatcher = await watchDebounced(this.configPath, {
@@ -336,8 +354,11 @@ class EdgeFunctionsRegistry {
336354
},
337355
})
338356

339-
// Creating a watcher for each source directory.
340-
await Promise.all(this.directories.map((directory) => this.setupWatcherForDirectory(directory)))
357+
// While functions are guaranteed to be inside one of the configured
358+
// directories, they might be importing files that are located in
359+
// parent directories. So we watch the entire project directory for
360+
// changes.
361+
await this.setupWatcherForDirectory(projectDir)
341362
}
342363

343364
async setupWatcherForDirectory(directory) {

src/utils/proxy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ const startProxy = async function ({
479479
geolocationMode,
480480
getUpdatedConfig,
481481
offline,
482+
projectDir,
482483
settings,
483484
state,
484485
})

0 commit comments

Comments
 (0)