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

incorrect routing for dynamic api routes when Next.js and Node runtime are used in same repo #7256

Open
williamli opened this issue Dec 31, 2021 · 1 comment
Labels
area: frameworks area: routing-utils triaged: bug Issue or PR has been reviewed by a maintainer and work is being tracked

Comments

@williamli
Copy link
Contributor

TLDR;

In a Next.js project with 2 dynamic API routes and 2 static API routes:

  1. /api/attachment/[id].js ✅
  2. /api/attachment/bye.js ✅
  3. /page/api/download/[id].js ☠️
  4. /page/api/download/hello.js ✅

All of the routes above works except for /api/download/something_other_than_hello. A 404 is returned instead.

Expected behaviour

https://vercel.com/support/articles/how-can-i-use-files-in-serverless-functions?query=includeFiles#next.js
The files inside api/* and pages/api/* are mapped to the /api/* subpath once deployed, with the api/* directory having precedence.

Workaround

A afterFiles rewrite has to be created in next.config.js to fix the broken route:

module.exports = {
  async rewrites() {
    return {
      afterFiles: [
        {
          source: '/api/download/:id',
          destination: '/api/download/[id]',
        },
      ]
    }
  },
}

Example Repo

https://github.com/vercel-support/63439-nextjs-mixed-apis/ deployed at https://63439-nextjs-mixed-apis.vercel-support.app/ (with the above next.config.js so everything works).

Something Interesting

In the deployment output, the route with problem is deployed without the file .js extension - /api/download/[id] while all other API routes end with .js.

image

@williamli williamli added triaged: bug Issue or PR has been reviewed by a maintainer and work is being tracked area: routing-utils area: frameworks labels Dec 31, 2021
@josharsh
Copy link

@williamli
May I pick this one this is still available?

kodiakhq bot pushed a commit to vercel/next.js that referenced this issue Mar 18, 2022
…5338)

Add test for `fs.readFile()`

- Related to vercel/vercel#7256
- Related to #32236

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
pavleadev pushed a commit to pavleadev/next.js that referenced this issue Mar 29, 2023
…5338)

Add test for `fs.readFile()`

- Related to vercel/vercel#7256
- Related to vercel/next.js#32236

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: frameworks area: routing-utils triaged: bug Issue or PR has been reviewed by a maintainer and work is being tracked
Projects
None yet
Development

No branches or pull requests

2 participants