How to Show 404 Page for Invalid Routes in dynamic routes ?
Answered
Bigeye scad posted this in #help-forum
Bigeye scadOP
I have pages only for specific routes like /education, /comics, and /books. If someone visits an invalid route like /note, I want it to show the 404 page. I already have a 404.js file. How can I make this work?
Answered by LuisLl
Even if you have that not-found.js file you’ll still render the page on demand for “/note”.
Unless you define
Unless you define
generateStaticParams()
that returns the params for pages you do want: [ {slug : “education”}, { slug : “comics” }, …. ] and export const dynamicParams = false
, to avoid generating new on demand like “/note”.18 Replies
@Bigeye scad I have pages only for specific routes like /education, /comics, and /books. If someone visits an invalid route like /note, I want it to show the 404 page. I already have a 404.js file. How can I make this work?
When you working inside the app router, then you don't need a 404.js. You need a "not-found.js" to make it work ([read more](https://nextjs.org/docs/app/api-reference/file-conventions/not-found))
Even if you have that not-found.js file you’ll still render the page on demand for “/note”.
Unless you define
Unless you define
generateStaticParams()
that returns the params for pages you do want: [ {slug : “education”}, { slug : “comics” }, …. ] and export const dynamicParams = false
, to avoid generating new on demand like “/note”.Answer
Bigeye scadOP
Thank you
@LuisLl is there any way to handle it in client side?
@Bigeye scad <@744377057139753020> is there any way to handle it in client side?
no, it can only run in server context
@B33fb0n3 When you working inside the app router, then you don't need a 404.js. You need a "not-found.js" to make it work ([read more](https://nextjs.org/docs/app/api-reference/file-conventions/not-found))
Dwarf Crocodile
Hey, i made a
is this approach good?
[...not-found]
folder in app directory, and made a custom 404 component inside it's page.jsx
.is this approach good?
something like:
import React from "react";
const Custom404 = () => {
return (
<div className="...">
Not Found
</div>
);
};
export default Custom404;
Dwarf Crocodile
whats wrong here
@Dwarf Crocodile whats wrong here
when moving the not-found into the app folder, it should work fine with the stuff luic mentioned
Dwarf Crocodile
this is my current structure.
Here the not-found is working fine.
Here the not-found is working fine.
inside dashbaord group, i have:
and when i put one more
not-found.jsx
inside admin folder, it was not working :/so i made a [...notfound] folder and put some custom 404
@Dwarf Crocodile this is my current structure.
Here the not-found is working fine.
you might want to open your own thread to share your specific issues and details about it
@Bigeye scad so, to conclude this thread:
Do what luis said here: https://nextjs-forum.com/post/1343547825048256583#message-1343593422694518855
TLDR: use
This can be done only serverside (https://nextjs-forum.com/post/1343547825048256583#message-1343849248223137824)
Is your initial issue solved like that ^?
Do what luis said here: https://nextjs-forum.com/post/1343547825048256583#message-1343593422694518855
TLDR: use
generateStaticParams
& dynamicParams = false
This can be done only serverside (https://nextjs-forum.com/post/1343547825048256583#message-1343849248223137824)
Is your initial issue solved like that ^?
@Bigeye scad solved?
Bigeye scadOP
yes @B33fb0n3