From dc7c4791280286c85fc5676ab1ee82db04567a12 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Mon, 12 Jun 2023 14:59:03 +0200 Subject: [PATCH] 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> --- cypress/e2e/default/api.cy.ts | 8 ++++++++ packages/runtime/src/helpers/config.ts | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cypress/e2e/default/api.cy.ts b/cypress/e2e/default/api.cy.ts index b0b24b9b4b..95f420bf92 100644 --- a/cypress/e2e/default/api.cy.ts +++ b/cypress/e2e/default/api.cy.ts @@ -6,6 +6,14 @@ describe('API routes', () => { }) }) +describe('Dynamic API routes', () => { + it('are bundled correctly', () => { + cy.request('/api/shows/1').then((response) => { + expect(response.status).to.equal(200) + }) + }) +}) + describe('Extended API routes', () => { it('returns HTTP 202 Accepted for background route', () => { cy.request('/api/hello-background').then((response) => { diff --git a/packages/runtime/src/helpers/config.ts b/packages/runtime/src/helpers/config.ts index 596a610dec..42198606f9 100644 --- a/packages/runtime/src/helpers/config.ts +++ b/packages/runtime/src/helpers/config.ts @@ -96,6 +96,12 @@ export const hasManuallyAddedModule = ({ ) /* eslint-enable camelcase */ +/** + * Transforms `/api/shows/[id].js` into `/api/shows/*id*.js`, + * so that the file `[id].js` is matched correctly. + */ +const escapeGlob = (path: string) => path.replace(/\[/g, '*').replace(/]/g, '*') + export const configureHandlerFunctions = async ({ netlifyConfig, publish, @@ -173,7 +179,7 @@ export const configureHandlerFunctions = async ({ netlifyConfig.functions[functionName] ||= { included_files: [] } netlifyConfig.functions[functionName].node_bundler = 'none' netlifyConfig.functions[functionName].included_files ||= [] - netlifyConfig.functions[functionName].included_files.push(...includedFiles) + netlifyConfig.functions[functionName].included_files.push(...includedFiles.map(escapeGlob)) } } else { configureFunction('_api_*')