Svelte & SvelteKit (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
Form Actions in SvelteKit: Progressive Enhancement without JavaScript.
senior LevelCode ExampleState ManagementFormsSystem Design
In Simple Words:
SvelteKit provides native Form Actions that handle data mutations via standard HTML <form> submissions:
typescript
// src/routes/login/+page.server.ts
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
const email = data.get('email');
await db.login(email);
return { success: true };
}
};
svelte
<!-- src/routes/login/+page.svelte -->
<script>
import { enhance } from '$app/forms';
</script>
<!-- Works with ZERO JS enabled! With JS enabled, use:enhance handles it via fetch() smoothly -->
<form method="POST" use:enhance>
<input name="email" type="email" required />
<button>Log In</button>
</form>