From 82e26ea4c8b11e277c1c167484987d783562ceb4 Mon Sep 17 00:00:00 2001 From: Netlify Team Account 1 Date: Tue, 5 Oct 2021 15:02:27 +0200 Subject: [PATCH 1/3] fix: decache all modules --- src/lib/functions/watcher.js | 42 ++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/lib/functions/watcher.js b/src/lib/functions/watcher.js index 93275bb0821..614dea56b69 100644 --- a/src/lib/functions/watcher.js +++ b/src/lib/functions/watcher.js @@ -5,34 +5,28 @@ const pEvent = require('p-event') const DEBOUNCE_WAIT = 100 -const watchDebounced = async (target, { depth, onAdd, onChange, onUnlink }) => { +const watchDebounced = async (target, { depth, onAdd = () => {}, onChange = () => {}, onUnlink = () => {} }) => { const watcher = chokidar.watch(target, { depth, ignored: /node_modules/, ignoreInitial: true }) await pEvent(watcher, 'ready') - const debouncedOnChange = debounce((path) => { - decache(path) - - if (typeof onChange === 'function') { - onChange(path) - } - }, DEBOUNCE_WAIT) - const debouncedOnUnlink = debounce((path) => { - decache(path) - - if (typeof onUnlink === 'function') { - onUnlink(path) - } - }, DEBOUNCE_WAIT) - const debouncedOnAdd = debounce((path) => { - decache(path) - - if (typeof onAdd === 'function') { - onAdd(path) - } - }, DEBOUNCE_WAIT) - - watcher.on('change', debouncedOnChange).on('unlink', debouncedOnUnlink).on('add', debouncedOnAdd) + const debouncedOnChange = debounce(onChange, DEBOUNCE_WAIT) + const debouncedOnUnlink = debounce(onUnlink, DEBOUNCE_WAIT) + const debouncedOnAdd = debounce(onAdd, DEBOUNCE_WAIT) + + watcher + .on('change', (path) => { + decache(path) + debouncedOnChange(path) + }) + .on('unlink', (path) => { + decache(path) + debouncedOnUnlink(path) + }) + .on('add', (path) => { + decache(path) + debouncedOnAdd(path) + }) return watcher } From 8da3f89d85eead6a352580ff1068706210b74f45 Mon Sep 17 00:00:00 2001 From: Netlify Team Account 1 Date: Tue, 5 Oct 2021 16:25:26 +0200 Subject: [PATCH 2/3] chore: re-run ci From 8473d621fa847851a722b28e7b36f5e10a36f6af Mon Sep 17 00:00:00 2001 From: Netlify Team Account 1 Date: Mon, 11 Oct 2021 11:14:37 +0200 Subject: [PATCH 3/3] chore: add reproduction test --- tests/serving-functions.test.js | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/serving-functions.test.js b/tests/serving-functions.test.js index caa4f4f1e57..eeef8657957 100644 --- a/tests/serving-functions.test.js +++ b/tests/serving-functions.test.js @@ -491,6 +491,60 @@ export { handler } }) }) + test(testName(`should pick up new function files even through debounce`, args), async (t) => { + await withSiteBuilder('function-file-updates', async (builder) => { + await builder + .withNetlifyToml({ + config: { + functions: { directory: 'functions' }, + }, + }) + .withContentFile({ + path: 'functions/hello/dist/index.js', + content: `module.exports = "foo"`, + }) + .withContentFile({ + path: 'functions/hello/dist/index.d.ts', + content: `export default "foo"`, + }) + .withContentFile({ + path: 'functions/hello/index.js', + content: ` +const response = require("./dist") +exports.handler = () => ({ + statusCode: 200, + body: response +})`, + }) + .buildAsync() + + await withDevServer({ cwd: builder.directory, args }, async (server) => { + const resp = await got.get(`${server.url}/.netlify/functions/hello`) + t.is(resp.body, 'foo') + + await builder + .withContentFile({ + path: 'functions/hello/dist/index.d.ts', + content: `export default "bar"`, + }) + .buildAsync() + + await builder + .withContentFile({ + path: 'functions/hello/dist/index.js', + content: `module.exports = "bar"`, + }) + .buildAsync() + + const DEBOUNCE_WAIT = 150 + await pause(DEBOUNCE_WAIT) + + const resp2 = await got.get(`${server.url}/.netlify/functions/hello`) + t.is(resp2.body, 'bar') + }) + }) + }) + test(testName('Serves functions from the internal functions directory', args), async (t) => { await withSiteBuilder('function-internal', async (builder) => { const bundlerConfig = args.includes('esbuild') ? { node_bundler: 'esbuild' } : {}