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

Catch-all route also catches static assets #67806

Closed
r6203 opened this issue Jul 16, 2024 · 8 comments
Closed

Catch-all route also catches static assets #67806

r6203 opened this issue Jul 16, 2024 · 8 comments
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@r6203
Copy link

r6203 commented Jul 16, 2024

Link to the code that reproduces this issue

https://github.com/r6203/next-catch-all-bug

To Reproduce

  1. build the app (next build)
  2. run the server (node .next/standalone/server.js)
  3. open the site in the browser and open dev tools => seeing that all static assets get resolved to the page.

Current vs. Expected behavior

Having a catch all route at the root of the app shouldn't intercept next generated assets.

Expect a working app where each non asset request resolves to the catch-all route but requests to next generated assets (_next/**/*) to resolve to the corresponding files.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Fri, 12 Jul 2024 00:06:53 +0000
  Available memory (MB): 32023
  Available CPU cores: 12
Binaries:
  Node: 22.4.1
  npm: 10.8.1
  Yarn: N/A
  pnpm: 9.5.0
Relevant Packages:
  next: 15.0.0-canary.68 // Latest available version is detected (15.0.0-canary.68).
  eslint-config-next: N/A
  react: 19.0.0-rc.0
  react-dom: 19.0.0-rc.0
  typescript: 5.3.3
Next.js Config:
  output: standalone

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

Runtime

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

next build (local)

Additional context

In development (next dev) everything is working fine. Issue seems to be related to the standalone server.

@r6203 r6203 added the bug Issue was opened via the bug report template. label Jul 16, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Jul 16, 2024
@whizzzkid
Copy link

This also seems to be happening on the pages router, /pages/[...catchAll]/index.ts seems to be intercepting /_next/.... routes.

One another observation it doesn't always happen, so might be a weird race condition somewhere, I am seeing intermittent 200s for such routes.

@avaneeshtripathi
Copy link

avaneeshtripathi commented Aug 6, 2024

Its there in canary.103 as well. Any workaround for the time being?? and doesn't seem to be intermittent.

@r6203
Copy link
Author

r6203 commented Aug 7, 2024

Its there in canary.103 as well. Any workaround for the time being?? and doesn't seem to be intermittent.

Hadn't found a workaround. As I was using next only for a marketing website I completely switched the framework.

@lstsystems
Copy link

lstsystems commented Aug 15, 2024

I encountered a similar issue with a CMS backend where the app/[...catch-all] route was also capturing the _next directory, especially when there were missing assets. I mitigated this by checking the slug path and returning early. However, this approach still returns a 200 status code even if a file is missing or the api endpoint is not there

const PageResolver = ({params}: PageProps) => {

    const firstSlug = params.slug[0];
    const reservedPaths = ['_next', 'api', 'sites'];
    if (reservedPaths.includes(firstSlug)) {
        return;
    }

    const slug = params.slug.join('/');
    return (
        <>
            <NodePages slug={slug} byAlias={true} />
        </>
    );
};

The problem is in the log now i get

 GET / 200 in 121ms
 GET /favicon.ico 200 in 10ms
 GET /sites/default/files/pictures/oem/ibm/printers/4610/2cr_cut-assy-wire.jpg 200 in 54ms
 ✓ Compiled in 291ms (883 modules)
 ✓ Compiled in 316ms (883 modules)

2cr_cut-assy-wire.jpg should have been 404 instead of 200, the browser still fails to load the image but i can't tell what else this return may be obfuscating at this time.

This is happening even in dev mode.

@Aerilym
Copy link
Contributor

Aerilym commented Oct 6, 2024

I'm encountering this issue too, the router catches all .js.map files and seems to override my public assets like favicons.

My workaround atm is to do the following:

// /constants.ts
export const NEXTJS_EXPLICIT_IGNORED_ROUTES = [
  'favicon.ico',
  'favicon-48x48.png',
  'favicon.svg',
  'apple-touch-icon.png',
  'site.webmanifest',
];

export const NEXTJS_IGNORED_PATTERNS = [
  '.js',
  '.mjs',
  '.map',
  '.css',
  '.json',
  '.png',
  '.jpg',
  '.jpeg',
  '.gif',
  '.webp',
  '.ico',
  '.svg',
];

Then in my page:

// /[slug]/page.tsx

export default async function UniversalPage({ params }: PageProps) {
  const slug = params.slug;

  if (
    NEXTJS_EXPLICIT_IGNORED_ROUTES.includes(slug) ||
    NEXTJS_IGNORED_PATTERNS.some((pattern) => slug.includes(pattern))
  ) {
    return;
  }

  return <div>Render this</div>
}

Happens in dev mode and when build. I feel like this is a pretty serious issue, there's surely a better way to handle this without a janky workaround. Would love to know if there's something I'm just missing or if this is a bug?

@github-actions github-actions bot added the linear: next Confirmed issue that is tracked by the Next.js team. label Oct 14, 2024
@vinicius-pretto
Copy link

I faced the same issue upgrading Next.js from v12 to v14.1.1. I tried v13.5 but was not lucky; the issue persists.

Any updates here?

@samcx
Copy link
Member

samcx commented Nov 6, 2024

Hi everyone—

With output: 'standalone', we automatically create a standalone folder that copies only the necessary files for a production deployment including select files in node_modules.

The minimal server does not copy the public or .next/static folders by default as these should ideally be handled by a CDN instead, although these folders can be copied to the standalone/public and standalone/.next/static folders manually, after which server.js file will serve these automatically.

To copy these and then start this local minimal server locally, you can do something like this:

pnpm build && cp -r .next/static .next/standalone/.next/ && node .next/standalone/server.js

I am not seeing any issues when the .next/static is moved.

I have also just created a PR to update our documentation with this → #72432.

I will closing this PR since this is a non-issue!

@samcx samcx closed this as completed Nov 6, 2024
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

7 participants