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

Conversation

Skn0tt
Copy link
Contributor

@Skn0tt Skn0tt commented Jun 5, 2023

Description

We discovered that dynamic API routes, e.g. /api/shows/[id].js, aren't bundled correctly. This is likely due to the square brackets being interpreted as special glob patterns, resulting in the underlying files not being included correctly.

This PR adds an e2e test that demonstrates the bug, and then fixes it by processing the glob patterns before passing them to the build process.

Documentation

Tests

You can test this change yourself like so:

  1. TODO

Relevant links (GitHub issues, etc.) or a picture of cute animal

https://github.com/netlify/pod-dev-foundations/issues/516

@Skn0tt Skn0tt requested a review from a team as a code owner June 5, 2023 11:48
@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/648715e2c6029d000842f192
😎 Deploy Preview https://deploy-preview-2154--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/648715e26b35e20008897bbb
😎 Deploy Preview https://deploy-preview-2154--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/648715e2b715c10008b96fa1
😎 Deploy Preview https://deploy-preview-2154--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/648715e2c72bc10007b30c0a
😎 Deploy Preview https://deploy-preview-2154--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/648715e28172e60008417cb2
😎 Deploy Preview https://deploy-preview-2154--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/648715e20fc2b70009341f3f
😎 Deploy Preview https://deploy-preview-2154--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Jun 5, 2023
@Skn0tt Skn0tt self-assigned this Jun 5, 2023
@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/648715e24ad8a20008fef0c9
😎 Deploy Preview https://deploy-preview-2154--next-plugin-canary.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/648715e2787b090008104e4a
😎 Deploy Preview https://deploy-preview-2154--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit 8f946a9
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/648715e2cf817300081256c7
😎 Deploy Preview https://deploy-preview-2154--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@Skn0tt
Copy link
Contributor Author

Skn0tt commented Jun 5, 2023

@Skn0tt
Copy link
Contributor Author

Skn0tt commented Jun 5, 2023

d19f422 should make it green 🟢

Copy link

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

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

The fix looks good, but I'm trying to understand how our existing tests didn't fail as we have other dynamic routes. Is the API route splitting only enabled on the default demo site?

Also, it'd be good to test with and without the split API routes feature flag as it's not rolled out to everyone yet.

Copy link

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

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

The fix looks good, but I'm trying to understand how our existing tests didn't fail as we have other dynamic routes. Is the API route splitting only enabled on the default demo site?

Also, it'd be good to test with and without the split API routes feature flag as it's not rolled out to everyone yet.

@Skn0tt
Copy link
Contributor Author

Skn0tt commented Jun 6, 2023

The fix looks good, but I'm trying to understand how our existing tests didn't fail as we have other dynamic routes.

The bug only exists for API Routes, not for SSR routes, since we're currently only using the new bundling mechanism for API Routes. I don't think we tested any other dynamic API Routes before, or else we'd have caught this bug earlier.

Is the API route splitting only enabled on the default demo site?

It's enabled in 4 demo sites, which I think are the sites that we're running e2e tests against: https://github.com/search?q=repo:netlify/next-runtime+NEXT_SPLIT_API_ROUTES&type=code

Also, it'd be good to test with and without the split API routes feature flag as it's not rolled out to everyone yet.

With the way our e2e tests work, I don't see how we can do that. I think that's out-of-scope for this PR as well. I'd prefer that we get the split-api-routes work done quickly, and remove the featureflag before it becomes long-lived enough to warrant additional test cases.

* 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.

@kodiakhq kodiakhq bot merged commit dc7c479 into main Jun 12, 2023
@kodiakhq kodiakhq bot deleted the fix-dynamic-api-routes branch June 12, 2023 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants