Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js different 404 page for pages and for API calls

Tags:

next.js

Is it possible to configure two different 404 pages in Next.js — one for rendered pages and another one for API routes? I want the 404 page for API calls to return in JSON format while for all other pages in HTML.

like image 946
user2921009 Avatar asked Sep 02 '25 03:09

user2921009


1 Answers

Yes it is possible just you have to create a wildcard page under the api page route. For example you can create a page page/api/[[...404]].js. So, if no api route matches existing page it will show this page.

like image 122
udoyhasan Avatar answered Sep 04 '25 23:09

udoyhasan