c/frontend › command
0votes

Graceful 404 Fallback

PromptLow risk Tested
submitted by CommandVault editors to c/frontend · 10 uses

Keep users oriented on bad URLs

Prompt · 2 variables
Add a graceful 404 experience to my app.

Stack: {{stack}}
Lost visitors should be steered to: {{goal}}

Requirements:
1. Catch every unmatched route (including nested and dynamic segments) and render a branded 404 page that returns a real HTTP 404 status, not a 200.
2. Show a short, human message, a primary button that takes people to {{goal}}, and a search box that searches the site.
3. Log the missing path to our error analytics, but strip query strings, emails, tokens and IDs before sending.
4. Keep the normal header and navigation so the page doesn't feel like a dead end.
5. Make it accessible: one h1, focus moves to the heading on load, the button and search work with the keyboard.

Before changing code, list the files you'll touch. After, give me:
- the exact steps to test it (a URL that should 404, what I should see, and how to confirm the status code in DevTools or with curl -I)
- anything you couldn't verify.
The page or action that helps them most, e.g. 'the dashboard and search' or 'the product catalog'
Framework, language and versions, e.g. Next.js 16 App Router + TypeScript
0 commentsreport

Guide

What you get: A real 404 page (HTTP 404, not 200) that keeps your header, sends people home, offers search, logs cleaned-up paths and is keyboard-friendly. Best for web apps with routing, where lost visitors currently see a generic or blank page.

Before you start

  • Fill in your stack (framework and version) and where lost visitors should go.
  • Let the AI run your build and lint, or run them yourself before trusting the result.
  • Optional: an error-analytics endpoint; without one, the logging is a no-op in production.

Worked example (Oct 17, 2026, Claude Code 2.1.296)

Input

Stack: Next.js 16.4 App Router + TypeScript (create-next-app starter, CSS modules)
Lost visitors should be steered to: the home page
(the rest of the prompt unchanged)

Result

Result after run 2 (build allowed), checked independently:
- npm run build, npm run lint and npx tsc --noEmit: all pass
- curl: / → 200; /does-not-exist → 404; /deeply/nested/missing/page → 404; /missing?token=abc&email=a@b.com → 404
- Page: one h1 ("We can't find that page"), focus lands on it on load, "Go to the home page" button, search box, normal header and nav, robots noindex
- Path cleaning: /users/42/orders/123456?token=abc → /users/:id/orders/:id; /invite/jane.doe@example.com → /invite/:email; /reset/<uuid> → /reset/:id; JWT-like segment → :token

Run 1, with file edits allowed but no shell commands, produced code that failed next build (a /search page broke Next.js 16 prerendering); Claude said it couldn't build or test. Run 2 allowed npm run build/lint and tsc, and passed every check above.

How to check it worked

  • curl -I https://your-site/this-does-not-exist → HTTP 404 (not 200 or a redirect).
  • The page keeps your header/navigation and the button goes to the right place.
  • Load the page with the keyboard only: focus starts on the heading; Tab reaches the button and search.
  • Check what your analytics receives: no query strings, emails, tokens or numeric IDs.

Common problems

  • Build fails after the change (seen in our first run): let the assistant run your build, or run it yourself and paste the error back.
  • Status is 200: a catch-all route or client-side redirect is swallowing the 404; use the framework's not-found mechanism.
  • Analytics never fires in production: no endpoint configured; set one or remove the call.

Try it here

Fill in {{goal}} above first for a better answer, or run it as is and the AI will assume sensible values.

or open in ChatGPT · Claude · Perplexity

How to use this prompt

  1. Fill in the 2 blanks above. The more specific you are, the better the answer.
  2. Copy it and paste it into ChatGPT, Claude, Gemini, Cursor or Codex, or use "Try it here" for a quick test.
  3. Read the answer critically: check anything it claims about your code or data before you rely on it.
  • {{goal}} The page or action that helps them most, e.g. 'the dashboard and search' or 'the product catalog'
  • {{stack}} Framework, language and versions, e.g. Next.js 16 App Router + TypeScript

More from c/frontend

See all of c/frontend →
0 comments

Log in to join the discussion.