## Demo: form-redirects ### FormRedirects.svelte ```svelte , …) from an action. With {@attach enhance(...)}, the JSON envelope is intercepted before navigating. Without it, the browser follows the native 303 response.`} {sources} > {#if redirected}

Redirected here via HTTP 303 (non-enhanced path).

{/if}

The action returns redirect(303, …). With {'{@attach enhance(...)}'} the JSON envelope is intercepted so you can inspect it before navigating. Without it, the browser follows the HTTP 303.

With {'{@attach enhance(...)}'}

Plain HTML

``` ### RedirectDemo.svelte ```svelte

{label}

{#if redirectResult}

Server returned type: "redirect"

status: {redirectResult.status}

location: "{redirectResult.location}"

{:else} {/if}
``` ### routes.ts ```ts import { Mochi, redirect, getRequestContext } from 'mochi-framework'; import type { MochiRouteValue } from 'mochi-framework'; export const routes: Record = { '/demos/form-redirects': Mochi.page('./src/demos/form-redirects/FormRedirects.svelte', { serverProps: () => { const { url } = getRequestContext(); return { redirected: url.searchParams.has('redirected') }; }, actions: { doRedirect: () => redirect(303, '/demos/form-redirects?redirected=1'), }, }), }; ``` ### index.ts ```ts import { Mochi, logger } from 'mochi-framework'; await Mochi.serve({ port: 3333, development: process.env.MODE === 'development', routes: { '/': Mochi.page('./src/Home.svelte'), }, }); logger.info('Server running at http://localhost:3333'); ```