This doc assumes you're storing your pages/styles/etc. in a directory called src
, and you have the baseUrl
option set to src
in your tsconfig.json
for importing modules from the root.
Handling 404s with is404
With Next.js, you can return { notFound: true }
from getServerSideProps
or getStaticProps
, which will signal to Next.js that you want to send a 404 Not Found
response. When building a Headless WordPress site, you want to send a 404 in the following conditions:
- The user visits a
post
route for which no WordPress post exists. - The user visits a
page
route for which no associated WordPress page (or custom frontend page) exists. - The user visits a
category
route for which no associated WordPress category exists.
To determine if you need to send a 404, you need to request WordPress for data and then decide if the response is enough to ensure you can render the page. Given that you will have dynamic Next.js pages for rendering posts
, pages
, and categories
most of the time, you might think you have to do this work yourself. However, Faust.js makes this easy for you.
Faust.js publishes an is404
function that makes this easy for you. It functions similar to how the usePost
, usePage
, and useCategory
hooks function, so it can determine the query you want to make based on URL params specified in your Next.js pages. The is404
function has the following logic:
is404
Logic for Posts:
- If
postId
is found in URL params,is404
makes a request to retrieve apost
from WordPress byID
. If nopost
is returned,is404
returnstrue
. - If
postSlug
is found in URL params,is404
makes a request to retrieve apost
from WordPress bySLUG
. If nopost
is returned,is404
returnstrue
. - If
postUri
is found in URL params,is404
requests to retrieve apost
from WordPress byURI
. If nopost
is returned,is404
returnstrue
.
is404
Logic for Pages:
- If
pageId
is found in URL params,is404
makes a request to retrieve apage
from WordPress byID
. If nopage
is returned,is404
returnstrue
. - If
pageUri
is found in URL params,is404
requests to retrieve apage
from WordPress byURI
. If nopage
is returned,is404
returnstrue
.
is404
Logic for Categories:
- If
categoryId
is found in URL params,is404
makes a request to retrieve acategory
from WordPress byID
. If nocategory
is returned,is404
returnstrue
. - If
categorySlug
is found in URL params,is404
requests to retrieve acategory
from WordPress bySLUG
. If nocategory
is returned,is404
returnstrue
.
How to use is404
is404
should be called in either getStaticProps
or getServerSideProps
. The reason for this is that these are the server-side functions where you need to tell Next.js to send a 404 Not Found
server response. Below is some code in the a post
page that utilizes is404
to return a { notFound: true }
if necessary:
import { getNextStaticProps, is404 } from '@faust/next';
import { client } from 'client';
export default function Page() {
return <></>;
}
export async function getStaticProps(context: GetStaticPropsContext) {
return getNextStaticProps(context, {
Page,
client,
notFound: await is404(context, { client }),
});
}
The above file name is [postSlug].tsx
which will ensure that postSlug
ends up in the URL params. When is404
is invoked, it will find the postSlug
URL param and make a request to WordPress to retrieve the post
by SLUG
using the postSlug
param. If no post
is returned, is404
will return true
and getStaticProps
will subsequently return { notFound: true }
.
NOTE:
is404
does not have any relation to the404.tsx
page you can define with Next.js. Customize your 404 page separately.