Fixed a URL order issue and optimized function for cloudfront #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Not sure if you still maintain this repo but I recently massively expanded the scope of URLs I was using with it and it started to cause unforeseen issues when deployed to Cloudfront. I noticed that the compute utilization was getting into the 70-100 range, but more importantly I noticed some routes were not getting routed correctly. I believe this is because the routes in the generated function are exactly as they appear in the Next file tree.
This causes a problem is cases such as the following rewrites list:
This list would be generated by an app folder with a
posts
folder with[:postId]
andfavorites
inside of it, each of those with their own dynamic folder inside them. The issue is that a request to/posts/favorites/:postId
would get caught by/posts/:postId/:commentId
and cause an error in the browser since the IDs would be invalid.To address both of these problems I took the rewrites list and turned it into a tree structure where the keys are the possible routes, destinations are marked with
$_d
and dynamic routes marked with$_p
. The tree structure can be injected as a string into the Cloudfront template. I updated that to use the tree structure as well. I noticed the compute utilization in cloudfront is down to the single digits for me since making this change.I copied the changes I made into a forked version of the repo and will leave them here as a PR. I won't claim they are fully tested, but can say this is what is working for my use case.