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

App crashes when using notFound() with dynamic routes #52255

Closed
1 task done
surjithctly opened this issue Jul 5, 2023 · 9 comments · Fixed by #54228
Closed
1 task done

App crashes when using notFound() with dynamic routes #52255

surjithctly opened this issue Jul 5, 2023 · 9 comments · Fixed by #54228
Assignees
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@surjithctly
Copy link

surjithctly commented Jul 5, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103
    Binaries:
      Node: 18.12.0
      npm: 8.19.2
      Yarn: 1.22.18
      pnpm: 8.6.5
    Relevant Packages:
      next: 13.4.9-canary.2
      eslint-config-next: 13.0.6
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true), Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://stackblitz.com/edit/stackblitz-starters-fxybus?file=app%2F(website)%2Fpage.js

To Reproduce

  1. Create a dynamic route eg: [id] and add the following code:
import { notFound } from "next/navigation";

export default function Page({ params }) {
  const user = await someFunction();

  if (!user) {
    notFound();
  }

  return <div>Page</div>;
}
  1. Now visit the slug with a 404 path.
  2. Next latest canary throws application error.

Describe the Bug

When using notFound(); with dynamic routes, next.js throws application error.

Application error: a server-side exception has occurred (see the server logs for more information).
Digest: NEXT_NOT_FOUND

Visit /aboutxyz in the above stackblitz repo to simulate the error.

Expected Behavior

It should not throw error. Instead it should show default next.js 404 error page.

Which browser are you using? (if relevant)

Chrome latest

How are you deploying your application? (if relevant)

vercel

NEXT-1532

@surjithctly surjithctly added the bug Issue was opened via the bug report template. label Jul 5, 2023
@github-actions github-actions bot added area: app App directory (appDir: true) Navigation Related to Next.js linking (e.g., <Link>) and navigation. labels Jul 5, 2023
@adrien1018
Copy link

adrien1018 commented Jul 17, 2023

The cause of this bug seems not to be dynamic routes, but instead layout under route groups. The error is not triggered if layout.tsx is moved from /app/(website) to /app.

Also, I can confirm this on Next.js versions from 13.3.1-canary.5 to 13.4.10. I highly suspect this is an unintended side-effect of #48286.
(In 13.4.10 the error becomes Error: Unsupported Server Component type: Null.)

@zougari47
Copy link

Having layout.tsx inside app/ fix this error for me.

@arekrgw
Copy link

arekrgw commented Jul 19, 2023

Same thing applies when I try to implement internationalization.

Using https://github.com/vercel/next.js/tree/canary/examples/app-dir-i18n-routing and adding simple notFound() on homepage throws nasty Error: Unsupported Server Component type: Null

Next version: 13.4.10

Hope it gets fixed soon

@sntiagomoreno
Copy link

I was facing the same error using a [locale] directory for internationalization. Fixed it by creating a custom not-found.tsx inside the [locale] directory. In case it helps.

@alvarlagerlof
Copy link

I was facing the same error using a [locale] directory for internationalization. Fixed it by creating a custom not-found.tsx inside the [locale] directory. In case it helps.

I tried this as well but it did not work. Might be because of the route groups.

@smitbarmase
Copy link
Contributor

+1 not-found not working for me in route groups

@zougari47
Copy link

+1 not-found not working for me in route groups

make sure you have layout.tsx in root of the app directory

@huozhi huozhi added the linear: next Confirmed issue that is tracked by the Next.js team. label Aug 18, 2023
@huozhi huozhi self-assigned this Aug 18, 2023
@kodiakhq kodiakhq bot closed this as completed in #54228 Aug 18, 2023
kodiakhq bot pushed a commit that referenced this issue Aug 18, 2023
For group routes, unlike normal routes, the root layout is located in the "group"'s segment instead of root layer.
To make it also able to leverage the default not found error component as root not found component, we need to determine if we're in the root group segment in the loader tree, and add the not-found boundary for it.


If you compre the loader tree visually they will look like this:

Normal route tree structure
```
['',
 { children: [segment, {...}, {...}]},
 { layout: ..., 'not-found': ... }
]
```

Group routes
```
[''
 { children: ['(group)', {...}, { layout, 'not-found': ... }]}
 {}
]
```

Comparing to normal case, we go 1 layer down in the tree for group routes, then insert the not-found boundary there

Fixes #52255
Closes NEXT-1532
@huozhi
Copy link
Member

huozhi commented Aug 19, 2023

This should be fixed in v13.4.19 now

@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2023

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 added the locked label Sep 2, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants