Part 13 of a series on Svelte in 2026. Part 12 covered routes, layouts and
load. This part is the other direction: sending data to the server with a form, first with no JavaScript at all, then with it.
I turned JavaScript off in Chromium, opened a SvelteKit todo page, typed “tea” and pressed Add. The page reloaded and “tea” was on the list, which is what a form is supposed to do and what a lot of JavaScript apps stopped doing years ago.
Then I pressed reload, and the list had two teas.
Both of those are the browser, not SvelteKit. A form action is a server function that a plain HTML form can post to, so it inherits everything forms have done since the nineties, including the duplicate. What SvelteKit adds is use:enhance, which takes the same form and runs it through fetch once JavaScript shows up. The interesting part is what changes when it does.
An action is a POST to the page
A +page.server.ts can export load, which part 12 covered, and actions. Each action is a function that gets the request, reads the form data, and returns whatever the page should see next:1
import { fail } from "@sveltejs/kit";import { todos, addTodo } from "#lib/server/db.js";import type { Actions, PageServerLoad } from "./$types";
export const load: PageServerLoad = async () => { return { todos: [...todos] };};
export const actions = { add: async ({ request }) => { const data = await request.formData(); const text = String(data.get("text") ?? "").trim(); if (!text) return fail(422, { text, missing: true }); addTodo(text); return { added: text }; },} satisfies Actions;The page posts to it with a form. ?/add picks the named action; a page with a single default action can leave action off entirely.
<script lang="ts"> import type { PageProps } from "./$types"; let { data, form }: PageProps = $props();</script>
{#if form?.missing}<p>Write something first.</p>{/if}{#if form?.added}<p>Added {form.added}</p>{/if}
<form method="POST" action="?/add"> <input name="text" value={form?.text ?? ""} /> <button>Add</button></form>
<ul> {#each data.todos as todo (todo.id)}<li>{todo.text}</li>{/each}</ul>I ran everything here on SvelteKit 3.1.0 with Svelte 5.57.2 and Vite 8.3.5, scaffolded with sv create, and drove it with Playwright 1.64.1 in Chromium.
With JavaScript disabled, pressing Add sent an ordinary application/x-www-form-urlencoded POST to /todos?/add. The server ran the action, then the page’s load, and sent back a full HTML page with “Added milk” on it. The browser stayed on /todos?/add, since nothing on the client was around to clean up the URL.
The value the action returns becomes the form prop, and it only lives for that one response. The docs call it ephemeral: reload the page with a GET and form is gone.1
You can’t mix the two kinds. I gave a page both a default and an other action, and the POST came back as a 500 with action_default_with_named in the terminal. The docs explain why: a named action’s ?/name stays in the URL, so the next default submit would quietly hit the named one instead.
A failed submit sends the form back
fail(422, { text, missing: true }) is how an action says no. With JavaScript off, an empty submit got an HTML page with HTTP status 422, the “Write something first.” message, and the <input> refilled from form.text. The browser treated a 422 page like any other page.
That refill is on you. The action decides what to send back, and the docs’ login example returns the email but not the password, on purpose.1 Whatever you return also goes through SvelteKit’s serializer: a Date came through as a Date, and an instance of my own class turned the submit into a 500 with action_data_not_serializable.
The page’s load ran after the failed action too: my load counter went up on every native submit, valid or not. That matters later, because use:enhance doesn’t do the same.
The second tea
Back to the duplicate. After a successful native POST, the page on screen is the response to that POST. Reload asks the browser to repeat the request that produced it, and Playwright’s page.reload() sent a POST to /todos?/add with the same body. My add counter went from 2 to 3, and “tea” was on the list twice.
In a desktop Chrome you’d get a “Confirm form resubmission” dialog first, and plenty of people click through it.
The fix is older than SvelteKit: redirect after the POST. In the action, replace return { added: text } with redirect(303, "/todos"), imported from @sveltejs/kit next to fail. I tried it on a copy of the page at /prg. With JavaScript off, the network log showed POST 303 /prg?/add followed by GET 200 /prg, the URL lost its ?/add, and a reload sent a GET that changed nothing.
The cost is the form prop. A redirect ends the POST response, so there’s no “Added tea” message on the page that follows, and if you want one you have to carry it some other way, a cookie or a query parameter. I’d still redirect on success for anything that creates or charges, and keep fail without a redirect for validation errors, where re-showing the form with the user’s input is the point.
use:enhance keeps the markup
Here’s the enhanced version of the same form:
<script lang="ts"> import { enhance } from "$app/forms"; // …same props as before</script>
<form method="POST" action="?/add" use:enhance> <input name="text" value={form?.text ?? ""} /> <button>Add</button></form>That’s the whole change. Without JavaScript, use:enhance is an attribute on an element that never gets hydrated, so the form behaves exactly as it did above. With JavaScript, SvelteKit intercepts the submit and sends it with fetch.
In the network tab, the request carried accept: application/json and x-sveltekit-action: true, and the response was JSON instead of a page: {"type":"success","status":200,"location":"/todos","data":…}. A failed submit came back with HTTP 422; before SvelteKit 3, enhanced responses always said 200.2
The difference that you can see is what stays alive. I clicked a counter button on the page three times and set a marker on window, then submitted.
On a page without use:enhance, JavaScript on, the form did a full native POST anyway: the counter went back to 0 and the marker was gone. SvelteKit doesn’t intercept POST forms you haven’t enhanced.
On the enhanced page, the counter still said 3, the marker was still there, the URL stayed /todos, and no mount or destroy logged. It’s part 12’s component reuse again: the page got new form and data props and kept its local $state.
By default it also emulates a reload, mostly.1 After a successful submit, the <input> was empty (the form gets reset), load ran again so the new todo showed up, and focus went back to <body>, the way it would after a real page load.
After a failed submit, load didn’t run: my counter stayed at 10 while page.status changed to 422. The docs spell it out: data is refreshed “on a successful response”, and refreshAll defaults to false for failures. If your load reads something the failed action changed anyway, that’s the case to watch.
Pending state only exists with JavaScript
Pass use:enhance a function and it runs right before the request goes out. Return another function and that one gets the result:
<script lang="ts"> import { enhance } from "$app/forms"; let submitting = $state(false);</script>
<form method="POST" action="?/add" use:enhance={() => { submitting = true; return async ({ update }) => { await update(); submitting = false; }; }}> <input name="text" /> <button disabled={submitting}>{submitting ? "Adding…" : "Add"}</button></form>Returning a callback replaces the default behaviour, so await update() is what puts it back: the reset, the form prop, the data refresh.1 Forget it and the submit succeeds on the server while the page shows nothing new.
With a 1.5-second delay in the action, the button read “Adding…” and was disabled 300ms in, then went back to “Add” when the response landed. Without JavaScript none of that code runs, and the only pending state is the browser’s own loading indicator. That’s the trade the whole approach makes: the baseline is plain, and everything nicer is an addition that can fail to load without taking the form down with it.
A form in a layout goes to its page
A newsletter box in the root layout can post to another route’s action with action="/newsletter?/subscribe". With JavaScript off, submitting it from /todos left me on /newsletter?/subscribe, with the newsletter page rendering “Subscribed ada@example.com” from its form prop.
In SvelteKit 3 the enhanced version does the same: it went to /newsletter, and the newsletter page showed the result. That’s new. SvelteKit 2 applied the result to the page you were on, and the migration guide calls the change a closer match to “the native, non-enhanced behaviour.”2
If you want the old behaviour for a footer widget, ask for it:
<form method="POST" action="/newsletter?/subscribe" use:enhance={() => async ({ update }) => { await update({ navigate: false }); }}>With that, the URL stayed /todos, and page.form in the layout held {"subscribed":"linus@example.com"}. That option only exists with JavaScript. Without it, the browser still navigates to the action’s page, so that page needs to render something useful from form too.
The dev server won’t show you CSRF
Any site can put a form on its page that posts to yours, and the browser will send it with your user’s cookies. SvelteKit blocks that by comparing the request’s Origin header with your app’s own.2
I sent a POST to /todos?/add with Origin: https://evil.example. On vite dev the action ran and added the todo. On a production build served with vite preview, the same request got a 403 and the text “Cross-site POST form submissions are forbidden”; a request with no Origin at all got a 403 too, and the same-origin one went through.
The check sits behind a !__SVELTEKIT_DEV__ guard in SvelteKit’s respond.js, so a dev-server test will never catch it. If you script submissions with curl against production, send an Origin.
SvelteKit 3 also removed csrf.checkOrigin: false, the old off switch. If another site of yours really needs to post forms to this one, list it in csrf.trustedOrigins, and behind a reverse proxy set paths.origin so SvelteKit knows what its own origin is.2
One more curl lesson: without Accept: text/html, SvelteKit answered my POSTs with the JSON an enhanced submit gets. Chromium’s native submit sent text/html first in its Accept header, so you only trip over this from the terminal.
React needs a server for this
React 19 lets you pass a function to <form action>, and in a client-only app that function runs only after the bundle loads. The no-JavaScript version needs a Server Function: the React docs say passing one lets users “submit forms without JavaScript enabled or before the code has loaded.”3 Showing a validation error before hydration also needs useActionState, and its permalink option exists for the case where the form posts before JavaScript arrives.4
In practice that means a framework with React Server Components, which today mostly means Next.js. I didn’t build the React side for this post; those claims are from the docs.
The two land in a similar place, a server function a form can call with or without JavaScript. SvelteKit’s version has fewer moving parts: an action is an exported function next to the page, its result is a prop, and the enhanced path is one attribute on markup that already worked.
What I’d take from this
Write the form so it works with JavaScript off, then add use:enhance. You get the reload-free version for almost nothing, and the native one is still underneath when a script fails to load on a train.
The two paths differ in ways you can test. A native submit loads a new page and drops local state, and its reload can re-send the POST unless the action redirects; an enhanced submit keeps the component, skips load on failure, and since SvelteKit 3 follows cross-page actions to their page. Turn JavaScript off in your browser’s devtools once per form and check both.
The form actions page covers everything this post used, and the SvelteKit 3 migration guide has the two behaviour changes. Its first note matters for the next part, too: the docs say new development is focused on the experimental form remote function, which covers the same ground with type safety.
Next: remote functions, and why they’re still behind a flag.
- Part 14 — Remote functions in SvelteKit: Server functions you import and call from a component, with the types carried across: what query, form and command generate, and why every export is a public endpoint
Footnotes
-
SvelteKit docs, Migrating to SvelteKit 3: the
$app/formschange, “Form action responses use thefailstatus code”, and “csrf.checkOriginreplaced bycsrf.trustedOrigins”. ↩ ↩2 ↩3 ↩4 -
React docs,
useActionState. ↩

