Skip to content
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

fix: decache all modules #3450

Merged
merged 5 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
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
42 changes: 18 additions & 24 deletions src/lib/functions/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
54 changes: 54 additions & 0 deletions tests/serving-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } : {}
Expand Down