sveltejs / sveltejs/kit

Support Importing Form Action Handlers with Automatic Path Resolution

Open
#13,754 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

forms
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

When creating forms in SvelteKit, the action attribute requires specifying the exact path as a string, for example:

// +page.server.ts

export const actions = {
  createTask: async () => {
    // ...
  }
}
// +page.svelte

<form method="POST" action="?/createTask">
  <!-- form fields -->
</form>

This approach means developers must manually manage and update form action paths, which can become error-prone and tedious, especially when reusing form components across different routes or modals. If the form’s route moves or if you want to use the same form in several places, the hardcoded string path needs to be updated in every instance.

Describe the proposed solution

Introduce a feature that allows developers to import a server action or handler and use it directly as the form's action attribute. SvelteKit would then resolve and inject the correct path at build or runtime. For example, instead of specifying a string path, the form could use:

<script>
import { actions } from './+page.server.ts'; // not allowed, because server code should not be imported on the client
// or import { createTask } from '$lib/server/actions';

</script>

<form method="POST" action={actions.createTask}>
  <!-- form fields -->
</form>

Internally, SvelteKit would map the imported handler to its correct route and inject the appropriate action path string.

Maybe a helper utility could also solve the problem, e.g. getActionPath(…)

Alternatives considered

Astro does something similar, https://docs.astro.build/en/guides/actions/

Importance

nice to have

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the +page.server.ts and +page.svelte examples, including the proposed $lib/server/actions import boundary, and trace how form action paths are currently represented. Define the supported import and route-resolution behavior before implementation; done means reusable forms can reference handlers without hardcoded paths and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
full-stack, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.