question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rewrites to /api paths are resulting in 404

See original GitHub issue

Describe 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

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:closed
  • Created 3 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
dphangcommented, Sep 15, 2021

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.

1reaction
dphangcommented, Sep 3, 2021

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found