Skip to content

fix: watch edge function dependencies #4576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lib/edge-functions/proxy.js
Original file line number Diff line number Diff line change
@@ -42,7 +42,16 @@ const handleProxyRequest = (req, proxyReq) => {
})
}

const initializeProxy = async ({ config, configPath, geolocationMode, getUpdatedConfig, offline, settings, state }) => {
const initializeProxy = async ({
config,
configPath,
geolocationMode,
getUpdatedConfig,
offline,
projectDir,
settings,
state,
}) => {
const { functions: internalFunctions, importMap, path: internalFunctionsPath } = await getInternalFunctions()
const { port: mainPort } = settings
const userFunctionsPath = config.build.edge_functions
@@ -60,6 +69,7 @@ const initializeProxy = async ({ config, configPath, geolocationMode, getUpdated
importMaps: [importMap].filter(Boolean),
internalFunctions,
port: isolatePort,
projectDir,
})
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctions.length !== 0

@@ -125,6 +135,7 @@ const prepareServer = async ({
importMaps,
internalFunctions,
port,
projectDir,
}) => {
const bundler = await import('@netlify/edge-bundler')
const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH])
@@ -148,6 +159,7 @@ const prepareServer = async ({
directories,
getUpdatedConfig,
internalFunctions,
projectDir,
runIsolate,
})

37 changes: 29 additions & 8 deletions src/lib/edge-functions/registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-check
const { fileURLToPath } = require('url')

const { NETLIFYDEVERR, NETLIFYDEVLOG, chalk, log, warn, watchDebounced } = require('../../utils/command-helpers')

/**
@@ -24,9 +26,19 @@ class EdgeFunctionsRegistry {
* @param {string[]} opts.directories
* @param {() => Promise<object>} opts.getUpdatedConfig
* @param {EdgeFunction[]} opts.internalFunctions
* @param {string} opts.projectDir
* @param {(functions: EdgeFunction[]) => Promise<object>} opts.runIsolate
*/
constructor({ bundler, config, configPath, directories, getUpdatedConfig, internalFunctions, runIsolate }) {
constructor({
bundler,
config,
configPath,
directories,
getUpdatedConfig,
internalFunctions,
projectDir,
runIsolate,
}) {
/**
* @type {import('@netlify/edge-bundler')}
*/
@@ -87,7 +99,7 @@ class EdgeFunctionsRegistry {
*/
this.initialScan = this.scan(directories)

this.setupWatchers()
this.setupWatchers({ projectDir })
}

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

graph.modules.forEach(({ dependencies = [], specifier }) => {
const functionMatch = functionPaths.get(specifier)
if (!specifier.startsWith('file://')) {
return
}

const path = fileURLToPath(specifier)
const functionMatch = functionPaths.get(path)

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

const { specifier: dependencyURL } = dependency.code
const functions = dependencyPaths.get(dependencyURL) || []
const dependencyPath = fileURLToPath(dependencyURL)
const functions = dependencyPaths.get(dependencyPath) || []

dependencyPaths.set(dependencyURL, [...functions, functionMatch])
dependencyPaths.set(dependencyPath, [...functions, functionMatch])
})
})

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

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

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

async setupWatcherForDirectory(directory) {
1 change: 1 addition & 0 deletions src/utils/proxy.js
Original file line number Diff line number Diff line change
@@ -479,6 +479,7 @@ const startProxy = async function ({
geolocationMode,
getUpdatedConfig,
offline,
projectDir,
settings,
state,
})