sveltejs / sveltejs/kit

Support FormData in SvelteKit remote function

Open
#14,403 1 comment 4 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

Currently, SvelteKit’s remote function works well with JSON payloads but does not provide direct support for sending FormData.
This becomes limiting when dealing with use cases that require file uploads (images, PDFs, etc.) or mixing text fields and binary data in the same request.

  • Enable seamless integration with forms that include file uploads.

  • Allow remote function to be used consistently for both JSON and FormData.

  • Avoid the need to fall back to manual fetch just for handling uploads.

Describe the proposed solution
import {  command } from '$app/server';

export const commandFormData = command(async (data: FormData) => {
    if (!(data instanceof FormData)) {
      throw new Error('Invalid form data')
    }
    const name = data.get('name')
    const age = data.get('age')
    const file = data.get('file')

    if (!name || !age) {
      throw new Error('Name and age are required')
    }

    return {
      name: name.toString(),
      age: parseInt(age.toString(), 10),
      file: file.name
    }
});
<script lang="ts">
	import { commandFormData } from './data.remote';
</script>

<button
	onclick={async () => {
		try {
                const formData = new FormData();
                formData.append('name', 'Name');
                await commandFormData(formData);
		} catch (error) {
			console.error(error);
		}
	}}
>
	Command with Form Data
</button>
Alternatives considered

No response

Importance

nice to have

Additional Information

https://tanstack.com/start/latest/docs/framework/react/server-functions

const yourFn = createServerFn({ method: 'POST' })
  .validator((formData) => {
    if (!(formData instanceof FormData)) {
      throw new Error('Invalid form data')
    }

    const name = formData.get('name')

    if (!name) {
      throw new Error('Name is required')
    }

    return name
  })
  .handler(async ({ data: name }) => {
    console.log(name) // 'John'
  })

console.info(yourFn.url)

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 tracing the remote function request path that currently handles JSON payloads and identify where FormData would need to be accepted. Add coverage for text fields, file data, and validation, then verify that a remote command can receive FormData without falling back to manual fetch.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.