Browse documentation
Documentation

Not-found pages

Edit this guide on GitHub

Declares a catch-all page that resolves every URL it serves to a not-found error.

An unmatched request produces a 404 through pathless layers, without rendering a layout. This macro registers a catch-all page that returns NotFoundError. A layout's error boundary can then render a custom not-found view, as described in the error guide.

With a path, the macro appends a {*rest} catch-all segment and expands to a page named not_found serving every method under that prefix. Register it like any other explicit-path page: pass not_found to RouterBuilder::page, or let discover collect it.

Without a path, the macro expands to a not_found module holding the catch-all page, deriving the prefix from the enclosing module like any other module_router! handler.

More specific routes win over the catch-all, which only serves URLs nothing else matches. A catch-all requires at least one segment, so the prefix URL itself (/ for a root fallback) is not covered and is served by its own page.

Examples

Module-derived path (in src/app/admin.rs under module_router!, this covers /admin/{*rest}):

use hearth::router::not_found;
not_found!();

A site-wide fallback with an explicit path, covering every URL no other route serves:

use hearth::router::{Router, not_found};

not_found!("/");

let router = Router::builder().page(not_found).build();

An explicit-path fallback for one subtree only, here /admin/{*rest}:

not_found!("/admin");