Rewrites to /api paths are resulting in 404
See original GitHub issueDescribe the bug
A configuration of next.config.js as follow:
module.exports = {
target: 'serverless',
rewrites: async () => [
{
source: '/:path*',
destination: '/:path*',
},
{
source: '/:path*',
destination: '/api/proxy/:path*',
},
],
}
A rewrite to an /api
path is not gonna work and it is returning 404 page.
Actual behavior
The current DefaultLambda is not taking in cosideration the manifest of apis, so can not detect if a rewrite is actually leading to a valid API.
Expected behavior
This is working in NextJS and it is actually a good workaround for incremental adoption using a proxy to point everything to the existing legacy system.
Steps to reproduce
Should be enough to create an hello world API under /pages/api
and a next.config.js
configuration with a rewrite pointing to the API endpoint path.
Versions
- OS/Environment: Manjaro Linux
- @sls-next/serverless-component version:@1.19.0-alpha.35
- Next.js version: v10.0.8-canary.5
Additional context
It is a deal breaker since this was last resort to workaround this issue: https://github.com/serverless-nextjs/serverless-next.js/issues/929
Issue Analytics
- State:
- Created 3 years ago
- Comments:14
Top Results From Across the Web
backend server path is defined in next.config.js rewrites() but it ...
js: backend server path is defined in next. config. js rewrites() but it returns 404 error - Stack Overflow. Stack Overflow for Teams...
Read more >Rewrites - next.config.js
fallback rewrites are checked/applied, these are applied before rendering the 404 page and after dynamic routes/all static assets have been checked. If you...
Read more >error while sending request to REST api virtual service ...
You fix this by opening the listen step in your VSM file, and changing the base path in there, so you don't get...
Read more >Using redirects - AWS Amplify Hosting
200 redirects (rewrites) are intended to show content from the ... example code to redirect paths under a folder that can't be found...
Read more >Static Rewrite on 404 to API Service? - Render community
/products/xyz doesn't exist, but upon 404 a rewrite rule kicks in to hit the backend service and return a page that does exist...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hm, I’m not quite sure why =/
Anyway I am working on moving API pages to be handled by the default lambda as well so that it’s possible to support cross rewrites. The first PR to consolidate API pages into the default lambda is done here and available in the latest alpha: https://github.com/serverless-nextjs/serverless-next.js/pull/1632 which is experimentally available with
build.separateApiLambda = false
. (See https://github.com/serverless-nextjs/serverless-next.js/blob/5d6ff2edbf53c0a7e109499d54c7a652ac648dfc/packages/e2e-tests/next-app-experimental/serverless.yml#L8-L8 for an example).And I just need to rework the rewrite logic. Might be worth investing time there since it’d be the ideal solution…hoping to get it out by end of the week. So maybe you can use the manual solution for some time first.
@alexduros ya I think we just allow behaviors for pages but I think we should just allow all kinds of paths… let me see if that makes sense