Skip to content

Commit dc7c479

Browse files
fix: bundle dynamic API routes correctly with split-api-routes (#2154)
* chore: add repro test * fix: include files that include [] --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 9195192 commit dc7c479

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

cypress/e2e/default/api.cy.ts

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ describe('API routes', () => {
66
})
77
})
88

9+
describe('Dynamic API routes', () => {
10+
it('are bundled correctly', () => {
11+
cy.request('/api/shows/1').then((response) => {
12+
expect(response.status).to.equal(200)
13+
})
14+
})
15+
})
16+
917
describe('Extended API routes', () => {
1018
it('returns HTTP 202 Accepted for background route', () => {
1119
cy.request('/api/hello-background').then((response) => {

packages/runtime/src/helpers/config.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ export const hasManuallyAddedModule = ({
9696
)
9797
/* eslint-enable camelcase */
9898

99+
/**
100+
* Transforms `/api/shows/[id].js` into `/api/shows/*id*.js`,
101+
* so that the file `[id].js` is matched correctly.
102+
*/
103+
const escapeGlob = (path: string) => path.replace(/\[/g, '*').replace(/]/g, '*')
104+
99105
export const configureHandlerFunctions = async ({
100106
netlifyConfig,
101107
publish,
@@ -173,7 +179,7 @@ export const configureHandlerFunctions = async ({
173179
netlifyConfig.functions[functionName] ||= { included_files: [] }
174180
netlifyConfig.functions[functionName].node_bundler = 'none'
175181
netlifyConfig.functions[functionName].included_files ||= []
176-
netlifyConfig.functions[functionName].included_files.push(...includedFiles)
182+
netlifyConfig.functions[functionName].included_files.push(...includedFiles.map(escapeGlob))
177183
}
178184
} else {
179185
configureFunction('_api_*')

0 commit comments

Comments
 (0)