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: bundle dynamic API routes correctly with split-api-routes #2154

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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, '*')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is going to match unwanted files, e.g. grid.js
is it possible to escape the path, e.g. \[id\].js ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried escaping the path, and glob patterns do allow escaping the brackets - but I couldn't get that to work properly with how ZISI interprets the globs. Maybe we could open a follow-up issue to investigate this.

Yes, this matches too many files - but I don't think that it'll be a big problem in practice.

Copy link

@LekoArts LekoArts Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have been fixed in v9 of glob: isaacs/node-glob#277 (comment)

Would it be reasonable to wait for a new ZISI release updating that dependency? If it would take too long than we could do this for now and create a follow-up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know it's fixed in v9! I'd prefer the follow-up. Let's open an issue for this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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