vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.96k stars 26.69k forks source link

Custom page extension + own sitemap/robots route bug - route source code returned instead of Response #66116

Open Git-I985 opened 3 months ago

Git-I985 commented 3 months ago

Link to the code that reproduces this issue

https://github.com/Git-I985/next-custom-page-extension-custom-sitemap-route-bug

To Reproduce

  1. Create blank next project
  2. Configure custom page extension, lets say ["x.ts", "x.tsx"]
  3. Create sitemap.xml directory in src/app
  4. Create file route.x.ts with content
    export const GET = () => new Response('It will return file content because route starts with "sitemap"')
  5. run npm run dev in terminal
  6. Open http://localhost:3000/sitemap.xml
  7. You will see export const GET = () => new Response('It will return file content because route starts with "sitemap"') instead of response text

Current vs. Expected behavior

I expect route.x.ts return me actual response instead of page with route source code

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Thu Aug 17 21:23:05 PDT 2023; root:xnu-10002.1.11~3/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 21.5.0
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.2.3
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware, Module Resolution, Navigation, Parallel & Intercepting Routes

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local)

Additional context

It reproduced only if there is word sitemap in the begining of the route name, also same issue reproduced with the name robots

No response

AbhiShake1 commented 3 months ago

this seems to be intentional. @huozhi could you please tell why you avoided checking for /route while checking for metadata route here?

Git-I985 commented 3 months ago

The crutch solution with rewrites

src/app/proxy-sitemap/route.ts

export function GET(request: Request) {
// ... your logic here
}

// next.config.js
const nextConfig = {
  rewrites: () => {
    return [
      {
        source: '/robots.txt',
        destination: '/proxy-robots',
      },
      {
        source: '/sitemap.xml',
        destination: '/proxy-sitemap',
      }
    ];
  }
 }

 module.exports = nextConfig