Any page that is server rendered can redirect to a 404 if the data is not available.
pages/username/index.js
export async function getServerSideProps({ query }) {
const { username } = query;
const userDoc = await getUserWithUsername(username);
// If no user, short circuit to 404 page
if (!userDoc) {
return {
notFound: true,
};
}
}
import Link from 'next/link';
export default function Custom404() {
return (
<main>
<h1>404 - That page does not seem to exist...</h1>
<iframe
src="https://giphy.com/embed/l2JehQ2GitHGdVG9y"
width="480"
height="362"
frameBorder="0"
allowFullScreen
></iframe>
<Link href="/">
<button className="btn-blue">Go home</button>
</Link>
</main>
);
}