I'm building a blog using React and Sanity and in my Routes, the NotFound page works perfectly with static URLs but I found that it doesn't actually redirects for links with bad slug in URL:
This is my App.js routes
<BrowserRouter>
<Routes>
<Route element={<NavRoute element={<Home/>}/>} path="/"/>
<Route element={<NavRoute element={<BlogPost/>}/>} path="blog/post/:slug"/>
<Route element={<NavRoute element={<Posts/>}/>} path="blog"/>
<Route element={<NavRoute element={<About/>}/>} path="about"/>
<Route element={<NotFound/>} path="*"/>
</Routes>
</BrowserRouter>
This how I fetch data in BlogPost.js
useEffect(() => {
sanityClient.fetch(`*[slug.current == "${slug}"]{
title,
_id,
slug,
mainImage{
asset->{
_id,
url
}
},
body,
"name": author->name,
"authorImage": author->image,
"date": publishedAt
}`).then((data) => setSinglePost(data[0])).catch(console.error);
}, [slug]);
Is there a way to handle dynamic routing 404 errors ?