{"success":true,"name":"use-redirect-and-notfound-in-next-js-not-hand-rolled-escapes","description":"A Next.js server page must bounce the user or handle a missing record","category":"ai-brimind","path":"ai-brimind/use-redirect-and-notfound-in-next-js-not-hand-rolled-escapes/SKILL.md","file":"SKILL.md","content":"---\nname: use-redirect-and-notfound-in-next-js-not-hand-rolled-escapes\ndescription: \"A Next.js server page must bounce the user or handle a missing record\"\n---\n\n# Use redirect() and notFound() in Next.js, Not Hand-Rolled Escapes\n\n## When to use\nProject is Next.js App Router, and server code needs to send the user elsewhere or show a 404, and a manual workaround is forming.\n\n## Example request\nreturning a <meta refresh> or a client component that router.pushes on mount → the server built-in redirect('/login') does it properly with the right status. — Server-side redirect built-in. | fetch found no record so the page renders 'undefined' everywhere → call notFound() and let not-found.tsx answer with a real 404. — Built-in 404 escape. | wrapping redirect() in try/catch and it 'mysteriously' stops working → redirect works by throwing; a catch block swallows it. Rethrow or move it out. — Redirect throws by design. | permanent URL change (old slug → new slug) → permanentRedirect / 308 tells browsers and search engines it moved for good. — Permanence is part of the signal.\n\n## Instructions\nAct like a developer who reaches for the framework's escape hatches before inventing one. When a Next.js Server Component, Server Action, or handler must interrupt normal rendering: 1) Name the case: user must go elsewhere → redirect('/path'); the change is permanent (old URLs, renamed slugs) → permanentRedirect; the requested record does not exist → notFound(). Each produces the correct HTTP status and hooks into the routing system — things a meta refresh or an on-mount router.push never does. 2) Know the mechanics: these functions work by THROWING a special value that Next.js catches. That means (a) code after them never runs, and (b) a surrounding try/catch will swallow them and break the behavior silently — keep them outside catch blocks or rethrow. 3) Do the check early: fetch the record, and if it is missing call notFound() immediately, before any JSX touches the data — no optional-chaining a half-empty page into existence. 4) Choose server over client: redirecting after a Server Action or during server render belongs in redirect(), not in a client effect that flashes the wrong page first. 5) If unsure whether a redirect is temporary or permanent, decide based on whether the old URL should ever work again — search engines listen to that answer. Only implement once the case is named (redirect / permanent / not-found), and explain why the built-in fits it — not just that the user ends up on the right screen.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #507\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}