Skip to content

Commit acdcbb6

Browse files
authored
fix: allow for in-source-config internal edge functions in proxy (#5311)
1 parent 6214768 commit acdcbb6

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

src/lib/edge-functions/internal.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@ export const getInternalFunctions = async () => {
3636
const stats = await stat(path)
3737

3838
if (!stats.isDirectory()) {
39-
throw new Error('Path is not a directory')
39+
throw new Error('Internal edge functions directory expected')
4040
}
41+
} catch {
42+
return {
43+
functions: [],
44+
path: null,
45+
}
46+
}
4147

48+
try {
4249
const manifestPath = join(path, 'manifest.json')
4350
const manifest = JSON.parse(await readFile(manifestPath))
4451

src/lib/edge-functions/proxy.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const initializeProxy = async ({
8888
port: isolatePort,
8989
projectDir,
9090
})
91-
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctions.length !== 0
91+
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctionsPath
9292

9393
return async (req) => {
9494
if (req.headers[headers.Passthrough] !== undefined || !hasEdgeFunctions) {

tests/integration/100.command.dev.test.cjs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,6 @@ test('should respect in-source configuration from edge functions', async (t) =>
751751
handler: () => new Response('Hello world'),
752752
name: 'hello',
753753
})
754-
.withEdgeFunction({
755-
config: () => ({ path: '/internal-1' }),
756-
handler: () => new Response('Hello from an internal function'),
757-
internal: true,
758-
name: 'internal',
759-
})
760754

761755
await builder.buildAsync()
762756

@@ -766,11 +760,6 @@ test('should respect in-source configuration from edge functions', async (t) =>
766760
t.is(res1.statusCode, 200)
767761
t.is(res1.body, 'Hello world')
768762

769-
const res2 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })
770-
771-
t.is(res2.statusCode, 200)
772-
t.is(res2.body, 'Hello from an internal function')
773-
774763
// wait for file watcher to be up and running, which might take a little
775764
// if we do not wait, the next file change will not be picked up
776765
await pause(500)
@@ -781,6 +770,53 @@ test('should respect in-source configuration from edge functions', async (t) =>
781770
handler: () => new Response('Hello world'),
782771
name: 'hello',
783772
})
773+
.buildAsync()
774+
775+
await waitForLogMatching('Reloaded edge function')
776+
777+
const res2 = await got(`http://localhost:${port}/hello-1`, { throwHttpErrors: false })
778+
779+
t.is(res2.statusCode, 404)
780+
781+
const res3 = await got(`http://localhost:${port}/hello-2`, { throwHttpErrors: false })
782+
783+
t.is(res3.statusCode, 200)
784+
t.is(res3.body, 'Hello world')
785+
})
786+
})
787+
})
788+
789+
test('should respect in-source configuration from internal edge functions', async (t) => {
790+
await withSiteBuilder('site-with-internal-edge-functions', async (builder) => {
791+
const publicDir = 'public'
792+
await builder
793+
.withNetlifyToml({
794+
config: {
795+
build: {
796+
publish: publicDir,
797+
},
798+
},
799+
})
800+
.withEdgeFunction({
801+
config: () => ({ path: '/internal-1' }),
802+
handler: () => new Response('Hello from an internal function'),
803+
internal: true,
804+
name: 'internal',
805+
})
806+
807+
await builder.buildAsync()
808+
809+
await withDevServer({ cwd: builder.directory }, async ({ port, waitForLogMatching }) => {
810+
const res1 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })
811+
812+
t.is(res1.statusCode, 200)
813+
t.is(res1.body, 'Hello from an internal function')
814+
815+
// wait for file watcher to be up and running, which might take a little
816+
// if we do not wait, the next file change will not be picked up
817+
await pause(500)
818+
819+
await builder
784820
.withEdgeFunction({
785821
config: () => ({ path: '/internal-2' }),
786822
handler: () => new Response('Hello from an internal function'),
@@ -791,23 +827,14 @@ test('should respect in-source configuration from edge functions', async (t) =>
791827

792828
await waitForLogMatching('Reloaded edge function')
793829

794-
const res3 = await got(`http://localhost:${port}/hello-1`, { throwHttpErrors: false })
795-
796-
t.is(res3.statusCode, 404)
797-
798-
const res4 = await got(`http://localhost:${port}/hello-2`, { throwHttpErrors: false })
799-
800-
t.is(res4.statusCode, 200)
801-
t.is(res4.body, 'Hello world')
802-
803-
const res5 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })
830+
const res2 = await got(`http://localhost:${port}/internal-1`, { throwHttpErrors: false })
804831

805-
t.is(res5.statusCode, 404)
832+
t.is(res2.statusCode, 404)
806833

807-
const res6 = await got(`http://localhost:${port}/internal-2`, { throwHttpErrors: false })
834+
const res3 = await got(`http://localhost:${port}/internal-2`, { throwHttpErrors: false })
808835

809-
t.is(res6.statusCode, 200)
810-
t.is(res6.body, 'Hello from an internal function')
836+
t.is(res3.statusCode, 200)
837+
t.is(res3.body, 'Hello from an internal function')
811838
})
812839
})
813840
})

0 commit comments

Comments
 (0)