0

I want to redirect from a custom 404 page to the home page with locale in the URL.

E.g: if the users type wrong URL localhost:3000/ja/news/wrong-uri user will be redirected to localhost:3000/ja/.

The problem is when nextjs redirect to custom a 404-page router.locale always equal 'vi' which is the default locale.

Btw my website use next build and next export (everything is static)

Please help

 const Custom404 = () => {
  const router = useRouter();

  useEffect(() => {
    console.log(router);
    router.replace(`/${router.locale}/`);
  });

  return null;
};

export default Custom404;

2 Answers2

0

Use it

export async function getStaticPaths() {
  const pagesWithSlugs = await getAllPagesWithSlugs();
  return {
    paths: pagesWithSlugs.edges.map(({node}) => `/${node.slug}`) || [],
    fallback: false,
  };
}
export default function Custom404() {
  return (
    <div>
       404 - Sorry could not find this page 
    </div>
  );
}
Vida Hedayati
  • 328
  • 1
  • 7
-1

I have found the way. I have to store locale in the localstorage ✅.