+page.server.js with actions
Define named and default action handlers in +page.server.js to process form data.
+page.server.js with actions is a free Sveltejs Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Form Actions Intro
SvelteKit lets you handle form submissions on the server via action handlers in +page.server.js.
Default Action
Export an actions object with a default handler for unnamed forms.
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
// ...
}
};Form Element
Your form posts to the current URL. No action attribute needed.
<form method="POST">
<input name="email" />
<button>Submit</button>
</form>Named Actions
Multiple actions can live in one file; reference them by name in the form action attribute.
export const actions = {
login: async (event) => { ... },
signup: async (event) => { ... }
};Calling Named Actions
Reference with ?/name on the form action.
<form method="POST" action="?/login">Reading Form Data
Use await request.formData() to read submitted fields.
const data = await request.formData();
const email = data.get("email");Returning Data
Return an object; it becomes form prop on the page.
return { success: true };Redirect After Success
Use redirect() to navigate after a successful action.
Validation Errors
Use fail() for client-side display of validation errors (next lessons).
Locals Available
You can access auth info via locals set in hooks.
Server-Only
Like load, actions run only on the server, so DB calls are safe.
Quick Check
Where do actions live?
Recap
Export an actions object from +page.server.js to handle POSTs. Use named actions for multiple forms.
Frequently asked questions
Is the “+page.server.js with actions” lesson free?
Yes — the full text of “+page.server.js with actions” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.
What will I learn in “+page.server.js with actions”?
Define named and default action handlers in +page.server.js to process form data. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “+page.server.js with actions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- +page.server.js with actions
- The use:enhance Directive
- Progressive Enhancement for Forms
- Handling Validation Errors in Actions