Redirect / 404 via Response

A loader can throw (or return) a Response to short-circuit the page render. The PRISM runtime detects this and surfaces the user's Response as the actual HTTP response — same status, headers, and body. This is bext's analogue of Remix's redirect() / notFound().

Try it

  • Render normally — loader returns { ok: true }.
  • Trigger a 302 — loader throws a Response with status: 302 and a Location header pointing at the server-action-form demo.
  • Trigger a 404 — loader throws a Response with status: 404 and a plain-text body.

The page component doesn't need to know any of this. From its perspective, only the success path renders; the rest is handled before Page() is called.

Source

// Loader can throw or return a Response. The PRISM runtime
         // detects this and short-circuits the page render with the matching
    // status, headers, and body — Remix-style.

    export async function loader({ request }: LoaderArgs) {
                const url = new URL(request.url);
                if (url.searchParams.get("redirect")) {
                                throw new Response(null, {
                                                status: 302,
                                                headers: { Location: "/examples/server-action-form" },
                                    });
                    }
                if (url.searchParams.get("missing")) {
                                throw new Response("Page not found in our backing store.\n", {
                                                status: 404,
                                                headers: { "Content-Type": "text/plain" },
                                    });
                    }
                return { ok: true };
    }
    export default function Page() {
                    return <p>Loaded normally.</p>;
    }

Loader output

{
  "ok": true
}