Custom 'not found' page in nextjs
Unanswered
Dwarf Crocodile posted this in #help-forum
Dwarf CrocodileOP
I have made a folder [...not-found] inside my admin folder, so if a user try to go to any other route, he see a not found page.
is this the correect appraoch?
is this the correect appraoch?
8 Replies
I’d like to know if you are implementing this because
not-found.tsx
wasn’t enough for you?@LuisLl I’d like to know if you are implementing *this* because `not-found.tsx` wasn’t enough for you?
Dwarf CrocodileOP
i tried that, but it was not working (or maybe I did something wrong), then implemented this approach
Fair enough.
The thing is that you need to explicitly call the
The thing is that you need to explicitly call the
notFound()
function in order to trigger the not-found.tsx
file per route segment. Otherwise the root not-found.tsx
will be rendered in place of any routes that Next.js couldn't find.@LuisLl Fair enough.
The thing is that you need to *explicitly* call the `notFound()` function in order to trigger the `not-found.tsx` file per route segment. Otherwise the root `not-found.tsx` will be rendered in place of any routes that Next.js couldn't find.
Dwarf CrocodileOP
are you saying I can only use:
and not like:
// inside admin/not-found.js
export default function NotFound() {
return ();
and not like:
export default function Abc() {
return ();
....for all of my
not-found.js
files?not-found.tsx
files should export default a component, I don’t think the name matters much since default exports can take whatever name you want.This component will be the fallback for those pages that weren’t found by Next.js at runtime. But only the
not-found.tsx
file at the very root will be displayed in these cases. If you want to trigger route level specific
not-found
files you need to call the notFound()
from the next/navigation
package in order to render the route level fallback.Makes sense?
Dwarf CrocodileOP
Suppose this is my structure:
you are saying I call notFound() in
/app
(dashboard)
/admin
/viewer
/[...not-found]
page.jsx
/user
/viewer
/[...not-found]
page.jsx
/helper
/viewer
/[...not-found]
page.jsx
you are saying I call notFound() in
page.jsx
of admin, user n helper. like:import { notFound } from 'next/navigation'
//then I check if the route exists or not, if it doesn't, i call the notFound().
Basically