Skip to content

Commit

Permalink
fix: bundle dynamic API routes correctly with split-api-routes (#2154)
Browse files Browse the repository at this point in the history
* chore: add repro test

* fix: include files that include []

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
Skn0tt and kodiakhq[bot] authored Jun 12, 2023
1 parent 9195192 commit dc7c479
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cypress/e2e/default/api.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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_*')
Expand Down

0 comments on commit dc7c479

Please sign in to comment.