Next.js Discord

Discord Forum

Having multiple not-found pages in Next15 App dir not working. (Help Needed)

Answered
Goldstripe sardinella posted this in #help-forum
Open in Discord
Avatar
Goldstripe sardinellaOP
Im trying to make a not-found page for different layouts in next 15 App directory but I keep seeing the root layout's not found component. only. My file tree looks something like this:

 
 /app
 |- layout.tsx
 |- page.tsx
 |- not-found.tsx
 |- /about
    |- layout.tsx
    |- page.tsx
    |- not-found.tsx
 |- /info
    |- layout.tsx
    |- page.tsx 
    |- not-found.tsx


(if neccessary, ask me to share the contents in these files tho i dont think they're much important since it's just simple contents inside them).

# ISSUE
When I'm in a path lets say about/randompage or info/randompage it shows me the Root layout's not-found component instead of the about one or info one depending on the path.

# What im trying to achieve
I want to make the not-found components work for the different routes.
Answered by Jboncz
Okay, give this a shot
Image
View full answer

52 Replies

Avatar
Goldstripe sardinellaOP
also, please ping me I do not mind if i get @mentioned.
Avatar
@Goldstripe sardinella What your showing as your example should work...

app/
├── int/
│ ├── not-found.js // This will be used for /int/ and its sub-routes
│ └── page.js
├── ext/
│ ├── not-found.js // This will be used for /ext/ and its sub-routes
│ └── page.js
└── not-found.js // This will be used for all other routes

Can you ensure your going to /int/blahblah and still seeing the root not-found page?
Avatar
Yeah I can’t reproduce it. Works fine in my project
Avatar
@Jboncz Yeah I can’t reproduce it. Works fine in my project
Avatar
Goldstripe sardinellaOP
ill make a github repo and share the link in a bit.
bc it seems weird
Avatar
Make sure to make it a min repro please, I need to be able to actually run in on my pc to help
Avatar
Goldstripe sardinellaOP
are you using next15 too?
Avatar
I did it in next14 and next15
im not that good on github yet my apologies
Avatar
Like, make sure I can clone the repo and run it, remove anything that requires api keys and such so I can actually run it
min repo would just be reproducing the issue without all the 'extra' stuff
Avatar
@Jboncz Like, make sure I can clone the repo and run it, remove anything that requires api keys and such so I can actually run it
Avatar
Goldstripe sardinellaOP
its a very simple project theres nothing in it really
Avatar
Gotcha, sounds good.
Avatar
Goldstripe sardinellaOP
its just pages with some random content or no content at all
not even buttons
Avatar
Gotcha, sounds good.
Your using the app dir right?
Avatar
Goldstripe sardinellaOP
👍
Avatar
@Jboncz Your using the app dir right?
Avatar
Goldstripe sardinellaOP
yep
app dir, next15 and typescript
Avatar
okay sounds good hit me up when you got it setup
Avatar
@Jboncz okay sounds good hit me up when you got it setup
Avatar
Goldstripe sardinellaOP
alright thanks.
Avatar
Goldstripe sardinellaOP
btw when you reply, please do @mention me so i can respond quicker
Avatar
Gotcha @Goldstripe sardinella looking now
Avatar
@Jboncz Gotcha <@708074274824650822> looking now
Avatar
Goldstripe sardinellaOP
alright
fyi, the reason i created layouts for the routes(info and about) is because initially I did not use layouts for both so I thought it might be because they need to have there own layouts, so I decided to make layouts for both
just for heads up since you might get confused on why they have there own layouts
Avatar
Nah, no issues with that. taking a look tho one sec
Avatar
Goldstripe sardinellaOP
👍
Avatar
Okay, give this a shot
Image
Answer
Avatar
Goldstripe sardinellaOP
alright let me try
its working just fine
thanks for the help
Avatar
for a quick tldr its because next sees routes that dont exsist as dynamic, so you have to call notFound()
Avatar
Goldstripe sardinellaOP
its kind of weird why it doesnt work with the one i made
Avatar
otherwise it calls the root.
Avatar
its cause of the dynamic routes.
To make the not-found pages work as expected:

1. The not-found UI is only shown when the notFound() function is called .
2. For dynamic routes, you'll need to explicitly check if the route exists and call notFound() if it doesn't.
3. For static routes (like /about/nonexistentpage), you'll need to implement catch-all routes if you want to handle arbitrary subroutes.
Avatar
Goldstripe sardinellaOP
👍 thank you so much
Avatar
np
Good luck!
Avatar
Goldstripe sardinellaOP
thanks have a good day
Avatar
Make sure to mark an answer please @Goldstripe sardinella
Avatar
Goldstripe sardinellaOP
how do i close this again i forgot lol
Avatar
right click this message app-> mark
Avatar
Goldstripe sardinellaOP
okthere we go
Avatar
ty 🙂 later
Avatar
Goldstripe sardinellaOP
thanks