Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Return a 404 from a Next.js page render method (ssr + client-side)
import React from 'react';
import ErrorPage from 'next/error';
// Allow to render the error page + return the correct error code with SSR
// It is a hack and has the disadvantage of logging this server-side
// A better solution is welcome.
export const render404Error = () => {
if (process.browser) {
return <ErrorPage statusCode={404} />;
const error = new Error('404 triggered');
// @ts-ignore necessary to return a 404
error.code = 'ENOENT';
throw error;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment